* Bring back rbuild build to be used until bug 6372 is fixed.
[reactos.git] / tools / winebuild / utils.c
1 /*
2 * Small utility functions for winebuild
3 *
4 * Copyright 2000 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
35 #endif
36 #ifdef HAVE_SYS_MMAN_H
37 #include <sys/mman.h>
38 #endif
39
40 #include "build.h"
41
42 #define MAX_TMP_FILES 8
43 static const char *tmp_files[MAX_TMP_FILES];
44 static unsigned int nb_tmp_files;
45
46 static const struct
47 {
48 const char *name;
49 enum target_cpu cpu;
50 } cpu_names[] =
51 {
52 { "i386", CPU_x86 },
53 { "i486", CPU_x86 },
54 { "i586", CPU_x86 },
55 { "i686", CPU_x86 },
56 { "i786", CPU_x86 },
57 { "x86_64", CPU_x86_64 },
58 { "sparc", CPU_SPARC },
59 { "alpha", CPU_ALPHA },
60 { "powerpc", CPU_POWERPC },
61 { "arm", CPU_ARM }
62 };
63
64 /* atexit handler to clean tmp files */
65 static void cleanup_tmp_files(void)
66 {
67 unsigned int i;
68 for (i = 0; i < MAX_TMP_FILES; i++) if (tmp_files[i]) unlink( tmp_files[i] );
69 }
70
71
72 void *xmalloc (size_t size)
73 {
74 void *res;
75
76 res = malloc (size ? size : 1);
77 if (res == NULL)
78 {
79 fprintf (stderr, "Virtual memory exhausted.\n");
80 exit (1);
81 }
82 return res;
83 }
84
85 void *xrealloc (void *ptr, size_t size)
86 {
87 void *res = realloc (ptr, size);
88 if (size && res == NULL)
89 {
90 fprintf (stderr, "Virtual memory exhausted.\n");
91 exit (1);
92 }
93 return res;
94 }
95
96 char *xstrdup( const char *str )
97 {
98 char *res = strdup( str );
99 if (!res)
100 {
101 fprintf (stderr, "Virtual memory exhausted.\n");
102 exit (1);
103 }
104 return res;
105 }
106
107 char *strupper(char *s)
108 {
109 char *p;
110 for (p = s; *p; p++) *p = toupper(*p);
111 return s;
112 }
113
114 int strendswith(const char* str, const char* end)
115 {
116 int l = strlen(str);
117 int m = strlen(end);
118 return l >= m && strcmp(str + l - m, end) == 0;
119 }
120
121 void fatal_error( const char *msg, ... )
122 {
123 va_list valist;
124 va_start( valist, msg );
125 if (input_file_name)
126 {
127 fprintf( stderr, "%s:", input_file_name );
128 if (current_line)
129 fprintf( stderr, "%d:", current_line );
130 fputc( ' ', stderr );
131 }
132 else fprintf( stderr, "winebuild: " );
133 vfprintf( stderr, msg, valist );
134 va_end( valist );
135 exit(1);
136 }
137
138 void fatal_perror( const char *msg, ... )
139 {
140 va_list valist;
141 va_start( valist, msg );
142 if (input_file_name)
143 {
144 fprintf( stderr, "%s:", input_file_name );
145 if (current_line)
146 fprintf( stderr, "%d:", current_line );
147 fputc( ' ', stderr );
148 }
149 vfprintf( stderr, msg, valist );
150 perror( " " );
151 va_end( valist );
152 exit(1);
153 }
154
155 void error( const char *msg, ... )
156 {
157 va_list valist;
158 va_start( valist, msg );
159 if (input_file_name)
160 {
161 fprintf( stderr, "%s:", input_file_name );
162 if (current_line)
163 fprintf( stderr, "%d:", current_line );
164 fputc( ' ', stderr );
165 }
166 vfprintf( stderr, msg, valist );
167 va_end( valist );
168 nb_errors++;
169 }
170
171 void warning( const char *msg, ... )
172 {
173 va_list valist;
174
175 if (!display_warnings) return;
176 va_start( valist, msg );
177 if (input_file_name)
178 {
179 fprintf( stderr, "%s:", input_file_name );
180 if (current_line)
181 fprintf( stderr, "%d:", current_line );
182 fputc( ' ', stderr );
183 }
184 fprintf( stderr, "warning: " );
185 vfprintf( stderr, msg, valist );
186 va_end( valist );
187 }
188
189 int output( const char *format, ... )
190 {
191 int ret;
192 va_list valist;
193
194 va_start( valist, format );
195 ret = vfprintf( output_file, format, valist );
196 va_end( valist );
197 if (ret < 0) fatal_perror( "Output error" );
198 return ret;
199 }
200
201 /* find a build tool in the path, trying the various names */
202 static char *find_tool( const char * const *names )
203 {
204 static char **dirs;
205 static unsigned int count, maxlen;
206
207 char *p, *file;
208 unsigned int i, len;
209 struct stat st;
210
211 if (!dirs)
212 {
213 char *path;
214
215 /* split the path in directories */
216
217 if (!getenv( "PATH" )) return NULL;
218 path = xstrdup( getenv( "PATH" ));
219 for (p = path, count = 2; *p; p++) if (*p == ':') count++;
220 dirs = xmalloc( count * sizeof(*dirs) );
221 count = 0;
222 dirs[count++] = p = path;
223 while (*p)
224 {
225 while (*p && *p != ':') p++;
226 if (!*p) break;
227 *p++ = 0;
228 dirs[count++] = p;
229 }
230 for (i = 0; i < count; i++) maxlen = max( maxlen, strlen(dirs[i])+2 );
231 }
232
233 while (*names)
234 {
235 len = strlen(*names) + sizeof(EXEEXT) + 1;
236 file = xmalloc( maxlen + len );
237
238 for (i = 0; i < count; i++)
239 {
240 strcpy( file, dirs[i] );
241 p = file + strlen(file);
242 if (p == file) *p++ = '.';
243 if (p[-1] != '/') *p++ = '/';
244 strcpy( p, *names );
245 strcat( p, EXEEXT );
246
247 if (!stat( file, &st ) && S_ISREG(st.st_mode) && (st.st_mode & 0111)) return file;
248 }
249 free( file );
250 names++;
251 }
252 return NULL;
253 }
254
255 const char *get_as_command(void)
256 {
257 if (!as_command)
258 {
259 if (target_alias)
260 {
261 as_command = xmalloc( strlen(target_alias) + sizeof("-as") );
262 strcpy( as_command, target_alias );
263 strcat( as_command, "-as" );
264 }
265 else
266 {
267 static const char * const commands[] = { "gas", "as", NULL };
268 if (!(as_command = find_tool( commands ))) as_command = xstrdup("as");
269 }
270
271 if (force_pointer_size)
272 {
273 const char *args = (target_platform == PLATFORM_APPLE) ?
274 ((force_pointer_size == 8) ? " -arch x86_64" : " -arch i386") :
275 ((force_pointer_size == 8) ? " --64" : " --32");
276 as_command = xrealloc( as_command, strlen(as_command) + strlen(args) + 1 );
277 strcat( as_command, args );
278 }
279 }
280 return as_command;
281 }
282
283 const char *get_ld_command(void)
284 {
285 if (!ld_command)
286 {
287 if (target_alias)
288 {
289 ld_command = xmalloc( strlen(target_alias) + sizeof("-ld") );
290 strcpy( ld_command, target_alias );
291 strcat( ld_command, "-ld" );
292 }
293 else
294 {
295 static const char * const commands[] = { "ld", "gld", NULL };
296 if (!(ld_command = find_tool( commands ))) ld_command = xstrdup("ld");
297 }
298
299 if (force_pointer_size)
300 {
301 const char *args;
302
303 switch (target_platform)
304 {
305 case PLATFORM_APPLE:
306 args = (force_pointer_size == 8) ? " -arch x86_64" : " -arch i386";
307 break;
308 case PLATFORM_FREEBSD:
309 args = (force_pointer_size == 8) ? " -m elf_x86_64" : " -m elf_i386_fbsd";
310 break;
311 default:
312 args = (force_pointer_size == 8) ? " -m elf_x86_64" : " -m elf_i386";
313 break;
314 }
315 ld_command = xrealloc( ld_command, strlen(ld_command) + strlen(args) + 1 );
316 strcat( ld_command, args );
317 }
318 }
319 return ld_command;
320 }
321
322 const char *get_nm_command(void)
323 {
324 if (!nm_command)
325 {
326 if (target_alias)
327 {
328 nm_command = xmalloc( strlen(target_alias) + sizeof("-nm") );
329 strcpy( nm_command, target_alias );
330 strcat( nm_command, "-nm" );
331 }
332 else
333 {
334 static const char * const commands[] = { "nm", "gnm", NULL };
335 if (!(nm_command = find_tool( commands ))) nm_command = xstrdup("nm");
336 }
337 }
338 return nm_command;
339 }
340
341 const char *get_windres_command(void)
342 {
343 static char *windres_command;
344
345 if (!windres_command)
346 {
347 if (target_alias)
348 {
349 windres_command = xmalloc( strlen(target_alias) + sizeof("-windres") );
350 strcpy( windres_command, target_alias );
351 strcat( windres_command, "-windres" );
352 }
353 else
354 {
355 static const char * const commands[] = { "windres", NULL };
356 if (!(windres_command = find_tool( commands ))) windres_command = xstrdup("windres");
357 }
358 }
359 return windres_command;
360 }
361
362 /* get a name for a temp file, automatically cleaned up on exit */
363 char *get_temp_file_name( const char *prefix, const char *suffix )
364 {
365 char *name;
366 const char *ext;
367 int fd;
368
369 assert( nb_tmp_files < MAX_TMP_FILES );
370 if (!nb_tmp_files && !save_temps) atexit( cleanup_tmp_files );
371
372 if (!prefix || !prefix[0]) prefix = "winebuild";
373 if (!suffix) suffix = "";
374 if (!(ext = strchr( prefix, '.' ))) ext = prefix + strlen(prefix);
375 name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
376 strcpy( name, "/tmp/" );
377 memcpy( name + 5, prefix, ext - prefix );
378 strcpy( name + 5 + (ext - prefix), ".XXXXXX" );
379 strcat( name, suffix );
380
381 /* first try without the /tmp/ prefix */
382 if ((fd = mkstemps( name + 5, strlen(suffix) )) != -1)
383 name += 5;
384 else if ((fd = mkstemps( name, strlen(suffix) )) == -1)
385 fatal_error( "could not generate a temp file\n" );
386
387 close( fd );
388 tmp_files[nb_tmp_files++] = name;
389 return name;
390 }
391
392 /*******************************************************************
393 * buffer management
394 *
395 * Function for reading from/writing to a memory buffer.
396 */
397
398 int byte_swapped = 0;
399 const char *input_buffer_filename;
400 const unsigned char *input_buffer;
401 size_t input_buffer_pos;
402 size_t input_buffer_size;
403 unsigned char *output_buffer;
404 size_t output_buffer_pos;
405 size_t output_buffer_size;
406
407 static void check_output_buffer_space( size_t size )
408 {
409 if (output_buffer_pos + size >= output_buffer_size)
410 {
411 output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
412 output_buffer = xrealloc( output_buffer, output_buffer_size );
413 }
414 }
415
416 void init_input_buffer( const char *file )
417 {
418 int fd;
419 struct stat st;
420
421 if ((fd = open( file, O_RDONLY | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file );
422 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", file );
423 if (!st.st_size) fatal_error( "%s is an empty file\n", file );
424 #ifdef HAVE_MMAP
425 if ((input_buffer = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
426 #endif
427 {
428 unsigned char *buffer = xmalloc( st.st_size );
429 if (read( fd, buffer, st.st_size ) != st.st_size) fatal_error( "Cannot read %s\n", file );
430 input_buffer = buffer;
431 }
432 close( fd );
433 input_buffer_filename = xstrdup( file );
434 input_buffer_size = st.st_size;
435 input_buffer_pos = 0;
436 byte_swapped = 0;
437 }
438
439 void init_output_buffer(void)
440 {
441 output_buffer_size = 1024;
442 output_buffer_pos = 0;
443 output_buffer = xmalloc( output_buffer_size );
444 }
445
446 void flush_output_buffer(void)
447 {
448 if (fwrite( output_buffer, 1, output_buffer_pos, output_file ) != output_buffer_pos)
449 fatal_error( "Error writing to %s\n", output_file_name );
450 free( output_buffer );
451 }
452
453 unsigned char get_byte(void)
454 {
455 if (input_buffer_pos >= input_buffer_size)
456 fatal_error( "%s is a truncated file\n", input_buffer_filename );
457 return input_buffer[input_buffer_pos++];
458 }
459
460 unsigned short get_word(void)
461 {
462 unsigned short ret;
463
464 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
465 fatal_error( "%s is a truncated file\n", input_buffer_filename );
466 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
467 if (byte_swapped) ret = (ret << 8) | (ret >> 8);
468 input_buffer_pos += sizeof(ret);
469 return ret;
470 }
471
472 unsigned int get_dword(void)
473 {
474 unsigned int ret;
475
476 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
477 fatal_error( "%s is a truncated file\n", input_buffer_filename );
478 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
479 if (byte_swapped)
480 ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
481 input_buffer_pos += sizeof(ret);
482 return ret;
483 }
484
485 void put_data( const void *data, size_t size )
486 {
487 check_output_buffer_space( size );
488 memcpy( output_buffer + output_buffer_pos, data, size );
489 output_buffer_pos += size;
490 }
491
492 void put_byte( unsigned char val )
493 {
494 check_output_buffer_space( 1 );
495 output_buffer[output_buffer_pos++] = val;
496 }
497
498 void put_word( unsigned short val )
499 {
500 if (byte_swapped) val = (val << 8) | (val >> 8);
501 put_data( &val, sizeof(val) );
502 }
503
504 void put_dword( unsigned int val )
505 {
506 if (byte_swapped)
507 val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
508 put_data( &val, sizeof(val) );
509 }
510
511 void put_qword( unsigned int val )
512 {
513 if (byte_swapped)
514 {
515 put_dword( 0 );
516 put_dword( val );
517 }
518 else
519 {
520 put_dword( val );
521 put_dword( 0 );
522 }
523 }
524
525 /* pointer-sized word */
526 void put_pword( unsigned int val )
527 {
528 if (get_ptr_size() == 8) put_qword( val );
529 else put_dword( val );
530 }
531
532 void align_output( unsigned int align )
533 {
534 size_t size = align - (output_buffer_pos % align);
535
536 if (size == align) return;
537 check_output_buffer_space( size );
538 memset( output_buffer + output_buffer_pos, 0, size );
539 output_buffer_pos += size;
540 }
541
542 /* output a standard header for generated files */
543 void output_standard_file_header(void)
544 {
545 if (spec_file_name)
546 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
547 else
548 output( "/* File generated automatically; do not edit! */\n" );
549 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
550 }
551
552 /* dump a byte stream into the assembly code */
553 void dump_bytes( const void *buffer, unsigned int size )
554 {
555 unsigned int i;
556 const unsigned char *ptr = buffer;
557
558 if (!size) return;
559 output( "\t.byte " );
560 for (i = 0; i < size - 1; i++, ptr++)
561 {
562 if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
563 else output( "0x%02x,", *ptr );
564 }
565 output( "0x%02x\n", *ptr );
566 }
567
568
569 /*******************************************************************
570 * open_input_file
571 *
572 * Open a file in the given srcdir and set the input_file_name global variable.
573 */
574 FILE *open_input_file( const char *srcdir, const char *name )
575 {
576 char *fullname;
577 FILE *file = fopen( name, "r" );
578
579 if (!file && srcdir)
580 {
581 fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 );
582 strcpy( fullname, srcdir );
583 strcat( fullname, "/" );
584 strcat( fullname, name );
585 file = fopen( fullname, "r" );
586 }
587 else fullname = xstrdup( name );
588
589 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
590 input_file_name = fullname;
591 current_line = 1;
592 return file;
593 }
594
595
596 /*******************************************************************
597 * close_input_file
598 *
599 * Close the current input file (must have been opened with open_input_file).
600 */
601 void close_input_file( FILE *file )
602 {
603 fclose( file );
604 free( input_file_name );
605 input_file_name = NULL;
606 current_line = 0;
607 }
608
609
610 /*******************************************************************
611 * remove_stdcall_decoration
612 *
613 * Remove a possible @xx suffix from a function name.
614 * Return the numerical value of the suffix, or -1 if none.
615 */
616 int remove_stdcall_decoration( char *name )
617 {
618 char *p, *end = strrchr( name, '@' );
619 if (!end || !end[1] || end == name) return -1;
620 /* make sure all the rest is digits */
621 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
622 *end = 0;
623 return atoi( end + 1 );
624 }
625
626
627 /*******************************************************************
628 * assemble_file
629 *
630 * Run a file through the assembler.
631 */
632 void assemble_file( const char *src_file, const char *obj_file )
633 {
634 const char *prog = get_as_command();
635 char *cmd;
636 int err;
637
638 cmd = xmalloc( strlen(prog) + strlen(obj_file) + strlen(src_file) + 6 );
639 sprintf( cmd, "%s -o %s %s", prog, obj_file, src_file );
640 if (verbose) fprintf( stderr, "%s\n", cmd );
641 err = system( cmd );
642 if (err) fatal_error( "%s failed with status %d\n", prog, err );
643 free( cmd );
644 }
645
646
647 /*******************************************************************
648 * alloc_dll_spec
649 *
650 * Create a new dll spec file descriptor
651 */
652 DLLSPEC *alloc_dll_spec(void)
653 {
654 DLLSPEC *spec;
655
656 spec = xmalloc( sizeof(*spec) );
657 spec->file_name = NULL;
658 spec->dll_name = NULL;
659 spec->init_func = NULL;
660 spec->main_module = NULL;
661 spec->type = SPEC_WIN32;
662 spec->base = MAX_ORDINALS;
663 spec->limit = 0;
664 spec->stack_size = 0;
665 spec->heap_size = 0;
666 spec->nb_entry_points = 0;
667 spec->alloc_entry_points = 0;
668 spec->nb_names = 0;
669 spec->nb_resources = 0;
670 spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
671 if (get_ptr_size() > 4)
672 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
673 else
674 spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
675 spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
676 spec->subsystem = 0;
677 spec->subsystem_major = 4;
678 spec->subsystem_minor = 0;
679 spec->entry_points = NULL;
680 spec->names = NULL;
681 spec->ordinals = NULL;
682 spec->resources = NULL;
683 return spec;
684 }
685
686
687 /*******************************************************************
688 * free_dll_spec
689 *
690 * Free dll spec file descriptor
691 */
692 void free_dll_spec( DLLSPEC *spec )
693 {
694 int i;
695
696 for (i = 0; i < spec->nb_entry_points; i++)
697 {
698 ORDDEF *odp = &spec->entry_points[i];
699 free( odp->name );
700 free( odp->export_name );
701 free( odp->link_name );
702 }
703 free( spec->file_name );
704 free( spec->dll_name );
705 free( spec->init_func );
706 free( spec->entry_points );
707 free( spec->names );
708 free( spec->ordinals );
709 free( spec->resources );
710 free( spec );
711 }
712
713
714 /*******************************************************************
715 * make_c_identifier
716 *
717 * Map a string to a valid C identifier.
718 */
719 const char *make_c_identifier( const char *str )
720 {
721 static char buffer[256];
722 char *p;
723
724 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
725 {
726 if (isalnum(*str)) *p = *str;
727 else *p = '_';
728 }
729 *p = 0;
730 return buffer;
731 }
732
733
734 /*******************************************************************
735 * get_stub_name
736 *
737 * Generate an internal name for a stub entry point.
738 */
739 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
740 {
741 static char buffer[256];
742 if (odp->name || odp->export_name)
743 {
744 char *p;
745 sprintf( buffer, "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
746 /* make sure name is a legal C identifier */
747 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
748 if (!*p) return buffer;
749 }
750 sprintf( buffer, "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
751 return buffer;
752 }
753
754 /* parse a cpu name and return the corresponding value */
755 enum target_cpu get_cpu_from_name( const char *name )
756 {
757 unsigned int i;
758
759 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
760 if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
761 return -1;
762 }
763
764 /*****************************************************************
765 * Function: get_alignment
766 *
767 * Description:
768 * According to the info page for gas, the .align directive behaves
769 * differently on different systems. On some architectures, the
770 * argument of a .align directive is the number of bytes to pad to, so
771 * to align on an 8-byte boundary you'd say
772 * .align 8
773 * On other systems, the argument is "the number of low-order zero bits
774 * that the location counter must have after advancement." So to
775 * align on an 8-byte boundary you'd say
776 * .align 3
777 *
778 * The reason gas is written this way is that it's trying to mimick
779 * native assemblers for the various architectures it runs on. gas
780 * provides other directives that work consistently across
781 * architectures, but of course we want to work on all arches with or
782 * without gas. Hence this function.
783 *
784 *
785 * Parameters:
786 * align -- the number of bytes to align to. Must be a power of 2.
787 */
788 unsigned int get_alignment(unsigned int align)
789 {
790 unsigned int n;
791
792 assert( !(align & (align - 1)) );
793
794 switch(target_cpu)
795 {
796 case CPU_x86:
797 case CPU_x86_64:
798 case CPU_SPARC:
799 case CPU_ARM:
800 if (target_platform != PLATFORM_APPLE) return align;
801 /* fall through */
802 case CPU_POWERPC:
803 case CPU_ALPHA:
804 n = 0;
805 while ((1u << n) != align) n++;
806 return n;
807 }
808 /* unreached */
809 assert(0);
810 return 0;
811 }
812
813 /* return the page size for the target CPU */
814 unsigned int get_page_size(void)
815 {
816 switch(target_cpu)
817 {
818 case CPU_x86: return 4096;
819 case CPU_x86_64: return 4096;
820 case CPU_POWERPC: return 4096;
821 case CPU_ARM: return 4096;
822 case CPU_SPARC: return 8192;
823 case CPU_ALPHA: return 8192;
824 }
825 /* unreached */
826 assert(0);
827 return 0;
828 }
829
830 /* return the size of a pointer on the target CPU */
831 unsigned int get_ptr_size(void)
832 {
833 switch(target_cpu)
834 {
835 case CPU_x86:
836 case CPU_POWERPC:
837 case CPU_SPARC:
838 case CPU_ALPHA:
839 case CPU_ARM:
840 return 4;
841 case CPU_x86_64:
842 return 8;
843 }
844 /* unreached */
845 assert(0);
846 return 0;
847 }
848
849 /* return the assembly name for a C symbol */
850 const char *asm_name( const char *sym )
851 {
852 static char buffer[256];
853
854 switch (target_platform)
855 {
856 case PLATFORM_APPLE:
857 case PLATFORM_WINDOWS:
858 if (sym[0] == '.' && sym[1] == 'L') return sym;
859 buffer[0] = '_';
860 strcpy( buffer + 1, sym );
861 return buffer;
862 default:
863 return sym;
864 }
865 }
866
867 /* return an assembly function declaration for a C function name */
868 const char *func_declaration( const char *func )
869 {
870 static char buffer[256];
871
872 switch (target_platform)
873 {
874 case PLATFORM_APPLE:
875 return "";
876 case PLATFORM_WINDOWS:
877 sprintf( buffer, ".def _%s; .scl 2; .type 32; .endef", func );
878 break;
879 default:
880 switch(target_cpu)
881 {
882 case CPU_ARM:
883 sprintf( buffer, ".type %s,%%function", func );
884 break;
885 default:
886 sprintf( buffer, ".type %s,@function", func );
887 break;
888 }
889 break;
890 }
891 return buffer;
892 }
893
894 /* output a size declaration for an assembly function */
895 void output_function_size( const char *name )
896 {
897 switch (target_platform)
898 {
899 case PLATFORM_APPLE:
900 case PLATFORM_WINDOWS:
901 break;
902 default:
903 output( "\t.size %s, .-%s\n", name, name );
904 break;
905 }
906 }
907
908 /* output the GNU note for non-exec stack */
909 void output_gnu_stack_note(void)
910 {
911 switch (target_platform)
912 {
913 case PLATFORM_WINDOWS:
914 case PLATFORM_APPLE:
915 break;
916 default:
917 switch(target_cpu)
918 {
919 case CPU_ARM:
920 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
921 break;
922 default:
923 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
924 break;
925 }
926 break;
927 }
928 }
929
930 /* return a global symbol declaration for an assembly symbol */
931 const char *asm_globl( const char *func )
932 {
933 static char buffer[256];
934
935 switch (target_platform)
936 {
937 case PLATFORM_APPLE:
938 sprintf( buffer, "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
939 return buffer;
940 case PLATFORM_WINDOWS:
941 sprintf( buffer, "\t.globl _%s\n_%s:", func, func );
942 return buffer;
943 default:
944 sprintf( buffer, "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
945 return buffer;
946 }
947 }
948
949 const char *get_asm_ptr_keyword(void)
950 {
951 switch(get_ptr_size())
952 {
953 case 4: return ".long";
954 case 8: return ".quad";
955 }
956 assert(0);
957 return NULL;
958 }
959
960 const char *get_asm_string_keyword(void)
961 {
962 switch (target_platform)
963 {
964 case PLATFORM_APPLE:
965 return ".asciz";
966 default:
967 return ".string";
968 }
969 }
970
971 const char *get_asm_short_keyword(void)
972 {
973 switch (target_platform)
974 {
975 default: return ".short";
976 }
977 }
978
979 const char *get_asm_rodata_section(void)
980 {
981 switch (target_platform)
982 {
983 case PLATFORM_APPLE: return ".const";
984 default: return ".section .rodata";
985 }
986 }
987
988 const char *get_asm_string_section(void)
989 {
990 switch (target_platform)
991 {
992 case PLATFORM_APPLE: return ".cstring";
993 default: return ".section .rodata";
994 }
995 }