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 static const char **tmp_files
;
43 static unsigned int nb_tmp_files
;
44 static unsigned int max_tmp_files
;
57 { "amd64", CPU_x86_64
},
58 { "x86_64", CPU_x86_64
},
59 { "sparc", CPU_SPARC
},
60 { "powerpc", CPU_POWERPC
},
64 /* atexit handler to clean tmp files */
65 static void cleanup_tmp_files(void)
68 for (i
= 0; i
< nb_tmp_files
; i
++) if (tmp_files
[i
]) unlink( tmp_files
[i
] );
72 void *xmalloc (size_t size
)
76 res
= malloc (size
? size
: 1);
79 fprintf (stderr
, "Virtual memory exhausted.\n");
85 void *xrealloc (void *ptr
, size_t size
)
87 void *res
= realloc (ptr
, size
);
88 if (size
&& res
== NULL
)
90 fprintf (stderr
, "Virtual memory exhausted.\n");
96 char *xstrdup( const char *str
)
98 char *res
= strdup( str
);
101 fprintf (stderr
, "Virtual memory exhausted.\n");
107 char *strupper(char *s
)
110 for (p
= s
; *p
; p
++) *p
= toupper(*p
);
114 int strendswith(const char* str
, const char* end
)
118 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
121 char *strmake( const char* fmt
, ... )
129 char *p
= xmalloc( size
);
131 n
= vsnprintf( p
, size
, fmt
, ap
);
133 if (n
== -1) size
*= 2;
134 else if ((size_t)n
>= size
) size
= n
+ 1;
140 struct strarray
*strarray_init(void)
142 struct strarray
*array
= xmalloc( sizeof(*array
) );
145 array
->str
= xmalloc( array
->max
* sizeof(*array
->str
) );
149 static void strarray_add_one( struct strarray
*array
, const char *str
)
151 if (array
->count
== array
->max
)
154 array
->str
= xrealloc( array
->str
, array
->max
* sizeof(*array
->str
) );
156 array
->str
[array
->count
++] = str
;
159 void strarray_add( struct strarray
*array
, ... )
164 va_start( valist
, array
);
165 while ((str
= va_arg( valist
, const char *))) strarray_add_one( array
, str
);
169 void strarray_addv( struct strarray
*array
, char * const *argv
)
171 while (*argv
) strarray_add_one( array
, *argv
++ );
174 void strarray_free( struct strarray
*array
)
180 void fatal_error( const char *msg
, ... )
183 va_start( valist
, msg
);
186 fprintf( stderr
, "%s:", input_file_name
);
188 fprintf( stderr
, "%d:", current_line
);
189 fputc( ' ', stderr
);
191 else fprintf( stderr
, "winebuild: " );
192 vfprintf( stderr
, msg
, valist
);
197 void fatal_perror( const char *msg
, ... )
200 va_start( valist
, msg
);
203 fprintf( stderr
, "%s:", input_file_name
);
205 fprintf( stderr
, "%d:", current_line
);
206 fputc( ' ', stderr
);
208 vfprintf( stderr
, msg
, valist
);
214 void error( const char *msg
, ... )
217 va_start( valist
, msg
);
220 fprintf( stderr
, "%s:", input_file_name
);
222 fprintf( stderr
, "%d:", current_line
);
223 fputc( ' ', stderr
);
225 vfprintf( stderr
, msg
, valist
);
230 void warning( const char *msg
, ... )
234 if (!display_warnings
) return;
235 va_start( valist
, msg
);
238 fprintf( stderr
, "%s:", input_file_name
);
240 fprintf( stderr
, "%d:", current_line
);
241 fputc( ' ', stderr
);
243 fprintf( stderr
, "warning: " );
244 vfprintf( stderr
, msg
, valist
);
248 int output( const char *format
, ... )
253 va_start( valist
, format
);
254 ret
= vfprintf( output_file
, format
, valist
);
256 if (ret
< 0) fatal_perror( "Output error" );
260 void spawn( struct strarray
*args
)
265 strarray_add_one( args
, NULL
);
267 for (i
= 0; args
->str
[i
]; i
++)
268 fprintf( stderr
, "%s%c", args
->str
[i
], args
->str
[i
+1] ? ' ' : '\n' );
270 if ((status
= spawnvp( _P_WAIT
, args
->str
[0], args
->str
)))
272 if (status
> 0) fatal_error( "%s failed with status %u\n", args
->str
[0], status
);
273 else fatal_perror( "winebuild" );
278 /* find a build tool in the path, trying the various names */
279 char *find_tool( const char *name
, const char * const *names
)
282 static unsigned int count
, maxlen
;
285 const char *alt_names
[2];
289 if (target_alias
) return strmake( "%s-%s", target_alias
, name
);
295 /* split the path in directories */
297 if (!getenv( "PATH" )) return NULL
;
298 path
= xstrdup( getenv( "PATH" ));
299 for (p
= path
, count
= 2; *p
; p
++) if (*p
== ':') count
++;
300 dirs
= xmalloc( count
* sizeof(*dirs
) );
302 dirs
[count
++] = p
= path
;
305 while (*p
&& *p
!= ':') p
++;
310 for (i
= 0; i
< count
; i
++) maxlen
= max( maxlen
, strlen(dirs
[i
])+2 );
322 len
= strlen(*names
) + sizeof(EXEEXT
) + 1;
323 file
= xmalloc( maxlen
+ len
);
325 for (i
= 0; i
< count
; i
++)
327 strcpy( file
, dirs
[i
] );
328 p
= file
+ strlen(file
);
329 if (p
== file
) *p
++ = '.';
330 if (p
[-1] != '/') *p
++ = '/';
334 if (!stat( file
, &st
) && S_ISREG(st
.st_mode
) && (st
.st_mode
& 0111)) return file
;
339 return xstrdup( name
);
342 struct strarray
*get_as_command(void)
344 struct strarray
*args
= strarray_init();
348 static const char * const commands
[] = { "gas", "as", NULL
};
349 as_command
= find_tool( "as", commands
);
351 strarray_add_one( args
, as_command
);
353 if (force_pointer_size
)
355 switch (target_platform
)
358 strarray_add( args
, "-arch", (force_pointer_size
== 8) ? "x86_64" : "i386", NULL
);
361 strarray_add_one( args
, (force_pointer_size
== 8) ? "--64" : "--32" );
368 struct strarray
*get_ld_command(void)
370 struct strarray
*args
= strarray_init();
374 static const char * const commands
[] = { "ld", "gld", NULL
};
375 ld_command
= find_tool( "ld", commands
);
377 strarray_add_one( args
, ld_command
);
379 if (force_pointer_size
)
381 switch (target_platform
)
384 strarray_add( args
, "-arch", (force_pointer_size
== 8) ? "x86_64" : "i386", NULL
);
386 case PLATFORM_FREEBSD
:
387 strarray_add( args
, "-m", (force_pointer_size
== 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL
);
390 strarray_add( args
, "-m", (force_pointer_size
== 8) ? "elf_x86_64" : "elf_i386", NULL
);
397 const char *get_nm_command(void)
401 static const char * const commands
[] = { "nm", "gnm", NULL
};
402 nm_command
= find_tool( "nm", commands
);
407 /* get a name for a temp file, automatically cleaned up on exit */
408 char *get_temp_file_name( const char *prefix
, const char *suffix
)
414 if (!nb_tmp_files
&& !save_temps
) atexit( cleanup_tmp_files
);
416 if (!prefix
|| !prefix
[0]) prefix
= "winebuild";
417 if (!suffix
) suffix
= "";
418 if (!(ext
= strchr( prefix
, '.' ))) ext
= prefix
+ strlen(prefix
);
419 name
= xmalloc( sizeof("/tmp/") + (ext
- prefix
) + sizeof(".XXXXXX") + strlen(suffix
) );
420 strcpy( name
, "/tmp/" );
421 memcpy( name
+ 5, prefix
, ext
- prefix
);
422 strcpy( name
+ 5 + (ext
- prefix
), ".XXXXXX" );
423 strcat( name
, suffix
);
425 /* first try without the /tmp/ prefix */
426 if ((fd
= mkstemps( name
+ 5, strlen(suffix
) )) != -1)
428 else if ((fd
= mkstemps( name
, strlen(suffix
) )) == -1)
429 fatal_error( "could not generate a temp file\n" );
432 if (nb_tmp_files
>= max_tmp_files
)
434 max_tmp_files
= max( 2 * max_tmp_files
, 8 );
435 tmp_files
= xrealloc( tmp_files
, max_tmp_files
* sizeof(tmp_files
[0]) );
437 tmp_files
[nb_tmp_files
++] = name
;
441 /*******************************************************************
444 * Function for reading from/writing to a memory buffer.
447 int byte_swapped
= 0;
448 const char *input_buffer_filename
;
449 const unsigned char *input_buffer
;
450 size_t input_buffer_pos
;
451 size_t input_buffer_size
;
452 unsigned char *output_buffer
;
453 size_t output_buffer_pos
;
454 size_t output_buffer_size
;
456 static void check_output_buffer_space( size_t size
)
458 if (output_buffer_pos
+ size
>= output_buffer_size
)
460 output_buffer_size
= max( output_buffer_size
* 2, output_buffer_pos
+ size
);
461 output_buffer
= xrealloc( output_buffer
, output_buffer_size
);
465 void init_input_buffer( const char *file
)
470 if ((fd
= open( file
, O_RDONLY
| O_BINARY
)) == -1) fatal_perror( "Cannot open %s", file
);
471 if ((fstat( fd
, &st
) == -1)) fatal_perror( "Cannot stat %s", file
);
472 if (!st
.st_size
) fatal_error( "%s is an empty file\n", file
);
474 if ((input_buffer
= mmap( NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0 )) == (void*)-1)
477 unsigned char *buffer
= xmalloc( st
.st_size
);
478 if (read( fd
, buffer
, st
.st_size
) != st
.st_size
) fatal_error( "Cannot read %s\n", file
);
479 input_buffer
= buffer
;
482 input_buffer_filename
= xstrdup( file
);
483 input_buffer_size
= st
.st_size
;
484 input_buffer_pos
= 0;
488 void init_output_buffer(void)
490 output_buffer_size
= 1024;
491 output_buffer_pos
= 0;
492 output_buffer
= xmalloc( output_buffer_size
);
495 void flush_output_buffer(void)
497 if (fwrite( output_buffer
, 1, output_buffer_pos
, output_file
) != output_buffer_pos
)
498 fatal_error( "Error writing to %s\n", output_file_name
);
499 free( output_buffer
);
502 unsigned char get_byte(void)
504 if (input_buffer_pos
>= input_buffer_size
)
505 fatal_error( "%s is a truncated file\n", input_buffer_filename
);
506 return input_buffer
[input_buffer_pos
++];
509 unsigned short get_word(void)
513 if (input_buffer_pos
+ sizeof(ret
) > input_buffer_size
)
514 fatal_error( "%s is a truncated file\n", input_buffer_filename
);
515 memcpy( &ret
, input_buffer
+ input_buffer_pos
, sizeof(ret
) );
516 if (byte_swapped
) ret
= (ret
<< 8) | (ret
>> 8);
517 input_buffer_pos
+= sizeof(ret
);
521 unsigned int get_dword(void)
525 if (input_buffer_pos
+ sizeof(ret
) > input_buffer_size
)
526 fatal_error( "%s is a truncated file\n", input_buffer_filename
);
527 memcpy( &ret
, input_buffer
+ input_buffer_pos
, sizeof(ret
) );
529 ret
= ((ret
<< 24) | ((ret
<< 8) & 0x00ff0000) | ((ret
>> 8) & 0x0000ff00) | (ret
>> 24));
530 input_buffer_pos
+= sizeof(ret
);
534 void put_data( const void *data
, size_t size
)
536 check_output_buffer_space( size
);
537 memcpy( output_buffer
+ output_buffer_pos
, data
, size
);
538 output_buffer_pos
+= size
;
541 void put_byte( unsigned char val
)
543 check_output_buffer_space( 1 );
544 output_buffer
[output_buffer_pos
++] = val
;
547 void put_word( unsigned short val
)
549 if (byte_swapped
) val
= (val
<< 8) | (val
>> 8);
550 put_data( &val
, sizeof(val
) );
553 void put_dword( unsigned int val
)
556 val
= ((val
<< 24) | ((val
<< 8) & 0x00ff0000) | ((val
>> 8) & 0x0000ff00) | (val
>> 24));
557 put_data( &val
, sizeof(val
) );
560 void put_qword( unsigned int val
)
574 /* pointer-sized word */
575 void put_pword( unsigned int val
)
577 if (get_ptr_size() == 8) put_qword( val
);
578 else put_dword( val
);
581 void align_output( unsigned int align
)
583 size_t size
= align
- (output_buffer_pos
% align
);
585 if (size
== align
) return;
586 check_output_buffer_space( size
);
587 memset( output_buffer
+ output_buffer_pos
, 0, size
);
588 output_buffer_pos
+= size
;
591 /* output a standard header for generated files */
592 void output_standard_file_header(void)
595 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name
);
597 output( "/* File generated automatically; do not edit! */\n" );
598 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
601 /* dump a byte stream into the assembly code */
602 void dump_bytes( const void *buffer
, unsigned int size
)
605 const unsigned char *ptr
= buffer
;
608 output( "\t.byte " );
609 for (i
= 0; i
< size
- 1; i
++, ptr
++)
611 if ((i
% 16) == 15) output( "0x%02x\n\t.byte ", *ptr
);
612 else output( "0x%02x,", *ptr
);
614 output( "0x%02x\n", *ptr
);
618 /*******************************************************************
621 * Open a file in the given srcdir and set the input_file_name global variable.
623 FILE *open_input_file( const char *srcdir
, const char *name
)
626 FILE *file
= fopen( name
, "r" );
630 fullname
= strmake( "%s/%s", srcdir
, name
);
631 file
= fopen( fullname
, "r" );
633 else fullname
= xstrdup( name
);
635 if (!file
) fatal_error( "Cannot open file '%s'\n", fullname
);
636 input_file_name
= fullname
;
642 /*******************************************************************
645 * Close the current input file (must have been opened with open_input_file).
647 void close_input_file( FILE *file
)
650 free( input_file_name
);
651 input_file_name
= NULL
;
656 /*******************************************************************
657 * remove_stdcall_decoration
659 * Remove a possible @xx suffix from a function name.
660 * Return the numerical value of the suffix, or -1 if none.
662 int remove_stdcall_decoration( char *name
)
664 char *p
, *end
= strrchr( name
, '@' );
665 if (!end
|| !end
[1] || end
== name
) return -1;
666 if (target_cpu
!= CPU_x86
) return -1;
667 /* make sure all the rest is digits */
668 for (p
= end
+ 1; *p
; p
++) if (!isdigit(*p
)) return -1;
670 return atoi( end
+ 1 );
674 /*******************************************************************
677 * Run a file through the assembler.
679 void assemble_file( const char *src_file
, const char *obj_file
)
681 struct strarray
*args
= get_as_command();
682 strarray_add( args
, "-o", obj_file
, src_file
, NULL
);
684 strarray_free( args
);
688 /*******************************************************************
691 * Create a new dll spec file descriptor
693 DLLSPEC
*alloc_dll_spec(void)
697 spec
= xmalloc( sizeof(*spec
) );
698 spec
->file_name
= NULL
;
699 spec
->dll_name
= NULL
;
700 spec
->init_func
= NULL
;
701 spec
->main_module
= NULL
;
702 spec
->type
= SPEC_WIN32
;
703 spec
->base
= MAX_ORDINALS
;
705 spec
->stack_size
= 0;
707 spec
->nb_entry_points
= 0;
708 spec
->alloc_entry_points
= 0;
710 spec
->nb_resources
= 0;
711 spec
->characteristics
= IMAGE_FILE_EXECUTABLE_IMAGE
;
712 if (get_ptr_size() > 4)
713 spec
->characteristics
|= IMAGE_FILE_LARGE_ADDRESS_AWARE
;
715 spec
->characteristics
|= IMAGE_FILE_32BIT_MACHINE
;
716 spec
->dll_characteristics
= IMAGE_DLLCHARACTERISTICS_NX_COMPAT
;
718 spec
->subsystem_major
= 4;
719 spec
->subsystem_minor
= 0;
720 spec
->entry_points
= NULL
;
722 spec
->ordinals
= NULL
;
723 spec
->resources
= NULL
;
728 /*******************************************************************
731 * Free dll spec file descriptor
733 void free_dll_spec( DLLSPEC
*spec
)
737 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
739 ORDDEF
*odp
= &spec
->entry_points
[i
];
741 free( odp
->export_name
);
742 free( odp
->link_name
);
744 free( spec
->file_name
);
745 free( spec
->dll_name
);
746 free( spec
->init_func
);
747 free( spec
->entry_points
);
749 free( spec
->ordinals
);
750 free( spec
->resources
);
755 /*******************************************************************
758 * Map a string to a valid C identifier.
760 const char *make_c_identifier( const char *str
)
762 static char buffer
[256];
765 for (p
= buffer
; *str
&& p
< buffer
+sizeof(buffer
)-1; p
++, str
++)
767 if (isalnum(*str
)) *p
= *str
;
775 /*******************************************************************
778 * Generate an internal name for a stub entry point.
780 const char *get_stub_name( const ORDDEF
*odp
, const DLLSPEC
*spec
)
785 if (odp
->name
|| odp
->export_name
)
788 buffer
= strmake( "__wine_stub_%s", odp
->name
? odp
->name
: odp
->export_name
);
789 /* make sure name is a legal C identifier */
790 for (p
= buffer
; *p
; p
++) if (!isalnum(*p
) && *p
!= '_') break;
791 if (!*p
) return buffer
;
794 buffer
= strmake( "__wine_stub_%s_%d", make_c_identifier(spec
->file_name
), odp
->ordinal
);
798 /* parse a cpu name and return the corresponding value */
799 enum target_cpu
get_cpu_from_name( const char *name
)
803 for (i
= 0; i
< sizeof(cpu_names
)/sizeof(cpu_names
[0]); i
++)
804 if (!strcmp( cpu_names
[i
].name
, name
)) return cpu_names
[i
].cpu
;
808 /*****************************************************************
809 * Function: get_alignment
812 * According to the info page for gas, the .align directive behaves
813 * differently on different systems. On some architectures, the
814 * argument of a .align directive is the number of bytes to pad to, so
815 * to align on an 8-byte boundary you'd say
817 * On other systems, the argument is "the number of low-order zero bits
818 * that the location counter must have after advancement." So to
819 * align on an 8-byte boundary you'd say
822 * The reason gas is written this way is that it's trying to mimick
823 * native assemblers for the various architectures it runs on. gas
824 * provides other directives that work consistently across
825 * architectures, but of course we want to work on all arches with or
826 * without gas. Hence this function.
830 * align -- the number of bytes to align to. Must be a power of 2.
832 unsigned int get_alignment(unsigned int align
)
836 assert( !(align
& (align
- 1)) );
843 if (target_platform
!= PLATFORM_APPLE
) return align
;
848 while ((1u << n
) != align
) n
++;
856 /* return the page size for the target CPU */
857 unsigned int get_page_size(void)
861 case CPU_x86
: return 4096;
862 case CPU_x86_64
: return 4096;
863 case CPU_POWERPC
: return 4096;
864 case CPU_ARM
: return 4096;
865 case CPU_SPARC
: return 8192;
872 /* return the size of a pointer on the target CPU */
873 unsigned int get_ptr_size(void)
890 /* return the total size in bytes of the arguments on the stack */
891 unsigned int get_args_size( const ORDDEF
*odp
)
893 unsigned int i
, size
;
895 for (i
= size
= 0; i
< odp
->u
.func
.nb_args
; i
++)
897 switch (odp
->u
.func
.args
[i
])
904 /* int128 is passed as pointer on x86_64 */
905 if (target_cpu
!= CPU_x86_64
)
912 size
+= get_ptr_size();
919 /* return the assembly name for a C symbol */
920 const char *asm_name( const char *sym
)
924 switch (target_platform
)
927 case PLATFORM_WINDOWS
:
928 if (sym
[0] == '.' && sym
[1] == 'L') return sym
;
930 buffer
= strmake( "_%s", sym
);
937 /* return an assembly function declaration for a C function name */
938 const char *func_declaration( const char *func
)
942 switch (target_platform
)
946 case PLATFORM_WINDOWS
:
948 buffer
= strmake( ".def _%s; .scl 2; .type 32; .endef", func
);
955 buffer
= strmake( ".type %s,%%function", func
);
958 buffer
= strmake( ".type %s,@function", func
);
966 /* output a size declaration for an assembly function */
967 void output_function_size( const char *name
)
969 switch (target_platform
)
972 case PLATFORM_WINDOWS
:
975 output( "\t.size %s, .-%s\n", name
, name
);
980 /* output a .cfi directive */
981 void output_cfi( const char *format
, ... )
985 if (!unwind_tables
) return;
986 va_start( valist
, format
);
987 fputc( '\t', output_file
);
988 vfprintf( output_file
, format
, valist
);
989 fputc( '\n', output_file
);
993 /* output the GNU note for non-exec stack */
994 void output_gnu_stack_note(void)
996 switch (target_platform
)
998 case PLATFORM_WINDOWS
:
1005 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
1008 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
1015 /* return a global symbol declaration for an assembly symbol */
1016 const char *asm_globl( const char *func
)
1018 static char *buffer
;
1021 switch (target_platform
)
1023 case PLATFORM_APPLE
:
1024 buffer
= strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func
, func
, func
);
1026 case PLATFORM_WINDOWS
:
1027 buffer
= strmake( "\t.globl _%s\n_%s:", func
, func
);
1030 buffer
= strmake( "\t.globl %s\n\t.hidden %s\n%s:", func
, func
, func
);
1036 const char *get_asm_ptr_keyword(void)
1038 switch(get_ptr_size())
1040 case 4: return ".long";
1041 case 8: return ".quad";
1047 const char *get_asm_string_keyword(void)
1049 switch (target_platform
)
1051 case PLATFORM_APPLE
:
1058 const char *get_asm_short_keyword(void)
1060 switch (target_platform
)
1062 default: return ".short";
1066 const char *get_asm_rodata_section(void)
1068 switch (target_platform
)
1070 case PLATFORM_APPLE
: return ".const";
1071 default: return ".section .rodata";
1075 const char *get_asm_string_section(void)
1077 switch (target_platform
)
1079 case PLATFORM_APPLE
: return ".cstring";
1080 default: return ".section .rodata";