Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 1 | #!/bin/sh |
Kevin O'Connor | 84452f1 | 2009-03-27 00:07:07 -0400 | [diff] [blame] | 2 | # Script to test if gcc "-fwhole-program" works properly. |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 3 | |
| 4 | mkdir -p out |
| 5 | TMPFILE1=out/tmp_testcompile1.c |
| 6 | TMPFILE1o=out/tmp_testcompile1.o |
Kevin O'Connor | 626416e | 2011-05-21 21:05:04 -0400 | [diff] [blame] | 7 | TMPFILE1_ld=out/tmp_testcompile1.lds |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 8 | TMPFILE2=out/tmp_testcompile2.c |
| 9 | TMPFILE2o=out/tmp_testcompile2.o |
| 10 | TMPFILE3o=out/tmp_testcompile3.o |
| 11 | |
Kevin O'Connor | 626416e | 2011-05-21 21:05:04 -0400 | [diff] [blame] | 12 | # Test if ld's alignment handling is correct. This is a known problem |
| 13 | # with the linker that ships with Ubuntu 11.04. |
| 14 | cat - > $TMPFILE1 <<EOF |
| 15 | const char v1[] __attribute__((section(".text.v1"))) = "0123456789"; |
| 16 | const char v2[] __attribute__((section(".text.v2"))) = "0123456789"; |
| 17 | EOF |
| 18 | cat - > $TMPFILE1_ld <<EOF |
| 19 | SECTIONS |
| 20 | { |
| 21 | .mysection 0x88f0 : { |
| 22 | . = 0x10 ; |
| 23 | *(.text.v1) |
| 24 | . = 0x20 ; |
| 25 | *(.text.v2) |
| 26 | . = 0x30 ; |
| 27 | } |
| 28 | } |
| 29 | EOF |
| 30 | $CC -O -g -c $TMPFILE1 -o $TMPFILE1o > /dev/null 2>&1 |
| 31 | $LD -T $TMPFILE1_ld $TMPFILE1o -o $TMPFILE2o > /dev/null 2>&1 |
| 32 | if [ $? -ne 0 ]; then |
| 33 | echo "The version of LD on this system does not properly handle" > /dev/fd/2 |
| 34 | echo "alignments. As a result, this project can not be built." > /dev/fd/2 |
| 35 | echo "" > /dev/fd/2 |
| 36 | echo "The problem may be the result of this LD bug report:" > /dev/fd/2 |
| 37 | echo " http://sourceware.org/bugzilla/show_bug.cgi?id=12726" > /dev/fd/2 |
| 38 | echo "" > /dev/fd/2 |
| 39 | echo "Please update to a working version of binutils and retry." > /dev/fd/2 |
| 40 | echo -1 |
| 41 | exit 0 |
| 42 | fi |
| 43 | |
Kevin O'Connor | ab233da | 2009-06-30 21:13:44 -0400 | [diff] [blame] | 44 | # Test for "-fwhole-program". Older versions of gcc (pre v4.1) don't |
| 45 | # support the whole-program optimization - detect that. |
Kevin O'Connor | 2787680 | 2009-03-27 00:12:00 -0400 | [diff] [blame] | 46 | $CC -fwhole-program -S -o /dev/null -xc /dev/null > /dev/null 2>&1 |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 47 | if [ $? -ne 0 ]; then |
Kevin O'Connor | 942d495 | 2009-06-10 22:44:06 -0400 | [diff] [blame] | 48 | echo " Working around no -fwhole-program" > /dev/fd/2 |
Kevin O'Connor | 0942e7f | 2009-06-15 22:27:01 -0400 | [diff] [blame] | 49 | echo 2 |
Kevin O'Connor | 942d495 | 2009-06-10 22:44:06 -0400 | [diff] [blame] | 50 | exit 0 |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 51 | fi |
| 52 | |
Kevin O'Connor | ab233da | 2009-06-30 21:13:44 -0400 | [diff] [blame] | 53 | # Test if "visible" variables and functions are marked global. On |
| 54 | # OpenSuse 10.3 "visible" variables declared with "extern" first |
| 55 | # aren't marked as global in the resulting assembler. On Ubuntu 7.10 |
| 56 | # "visible" functions aren't marked as global in the resulting |
| 57 | # assembler. |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 58 | cat - > $TMPFILE1 <<EOF |
| 59 | void __attribute__((externally_visible)) t1() { } |
Kevin O'Connor | a68287f | 2009-06-26 18:55:50 -0400 | [diff] [blame] | 60 | extern unsigned char v1; |
Kevin O'Connor | 0942e7f | 2009-06-15 22:27:01 -0400 | [diff] [blame] | 61 | unsigned char v1 __attribute__((section(".data16.foo.19"))) __attribute__((externally_visible)); |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 62 | EOF |
Kevin O'Connor | 84452f1 | 2009-03-27 00:07:07 -0400 | [diff] [blame] | 63 | $CC -Os -c -fwhole-program $TMPFILE1 -o $TMPFILE1o > /dev/null 2>&1 |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 64 | cat - > $TMPFILE2 <<EOF |
| 65 | void t1(); |
Kevin O'Connor | 0942e7f | 2009-06-15 22:27:01 -0400 | [diff] [blame] | 66 | extern unsigned char v1; |
| 67 | int __attribute__((externally_visible)) main() { t1(); return v1; } |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 68 | EOF |
Kevin O'Connor | 84452f1 | 2009-03-27 00:07:07 -0400 | [diff] [blame] | 69 | $CC -Os -c -fwhole-program $TMPFILE2 -o $TMPFILE2o > /dev/null 2>&1 |
Kevin O'Connor | ff82b24 | 2009-04-19 21:30:48 -0400 | [diff] [blame] | 70 | $CC -nostdlib -Os $TMPFILE1o $TMPFILE2o -o $TMPFILE3o > /dev/null 2>&1 |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 71 | if [ $? -ne 0 ]; then |
Kevin O'Connor | 0942e7f | 2009-06-15 22:27:01 -0400 | [diff] [blame] | 72 | echo " Working around non-functional -fwhole-program" > /dev/fd/2 |
| 73 | echo 2 |
| 74 | exit 0 |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 75 | fi |
| 76 | |
Kevin O'Connor | ab233da | 2009-06-30 21:13:44 -0400 | [diff] [blame] | 77 | # Test if "-combine" works. On Ubuntu 8.04 the compiler doesn't work |
| 78 | # correctly with combine and the "struct bregs" register due to the |
Kevin O'Connor | 3133e38 | 2009-11-20 09:19:28 -0500 | [diff] [blame] | 79 | # anonymous unions and structs. On Fedora Core 12 the compiler throws |
| 80 | # an internal compiler error when multiple files access global |
| 81 | # variables with debugging enabled. |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 82 | cat - > $TMPFILE1 <<EOF |
Kevin O'Connor | 3133e38 | 2009-11-20 09:19:28 -0500 | [diff] [blame] | 83 | // Look for anonymous union/struct failure |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 84 | struct ts { union { int u1; struct { int u2; }; }; }; |
Kevin O'Connor | 3133e38 | 2009-11-20 09:19:28 -0500 | [diff] [blame] | 85 | void func1(struct ts *r); |
| 86 | |
| 87 | // Look for global variable failure. |
| 88 | struct s1_s { int v; } g1; |
| 89 | void __attribute__((externally_visible)) func2() { |
| 90 | struct s1_s *l1 = &g1; |
Kevin O'Connor | 8918989 | 2010-01-03 15:12:00 -0500 | [diff] [blame] | 91 | l1->v=0; |
Kevin O'Connor | 3133e38 | 2009-11-20 09:19:28 -0500 | [diff] [blame] | 92 | } |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 93 | EOF |
Kevin O'Connor | 3133e38 | 2009-11-20 09:19:28 -0500 | [diff] [blame] | 94 | cat - > $TMPFILE2 <<EOF |
| 95 | struct ts { union { int u1; struct { int u2; }; }; }; |
| 96 | void func1(struct ts *r); |
| 97 | |
| 98 | extern struct s1_s g1; |
| 99 | void func3() { |
| 100 | &g1; |
| 101 | } |
| 102 | EOF |
| 103 | $CC -O -g -fwhole-program -combine -c $TMPFILE1 $TMPFILE2 -o $TMPFILE1o > /dev/null 2>&1 |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 104 | if [ $? -eq 0 ]; then |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 105 | echo 0 |
| 106 | else |
Kevin O'Connor | 4d863fe | 2009-06-15 22:45:00 -0400 | [diff] [blame] | 107 | echo " Working around non-functional -combine" > /dev/fd/2 |
Kevin O'Connor | 03f630e | 2009-03-26 20:45:36 -0400 | [diff] [blame] | 108 | echo 1 |
| 109 | fi |
| 110 | |
Kevin O'Connor | 3133e38 | 2009-11-20 09:19:28 -0500 | [diff] [blame] | 111 | # Also, on several compilers, -combine fails if code is emitted with a |
| 112 | # reference to an extern variable that is later found to be externally |
| 113 | # visible - the compiler does not mark those variables as global. |
| 114 | # This is being worked around by ordering the compile objects to avoid |
| 115 | # this case. |
| 116 | |
Kevin O'Connor | ab233da | 2009-06-30 21:13:44 -0400 | [diff] [blame] | 117 | # Also, the Ubuntu 8.04 compiler has a bug causing corruption when the |
| 118 | # "ebp" register is clobberred in an "asm" statement. The code has |
| 119 | # been modified to not clobber "ebp" - no test is available yet. |
| 120 | |
Kevin O'Connor | 626416e | 2011-05-21 21:05:04 -0400 | [diff] [blame] | 121 | rm -f $TMPFILE1 $TMPFILE1o $TMPFILE1_ld $TMPFILE2 $TMPFILE2o $TMPFILE3o |