blob: 935f211b0a2f6899846cfdb2a875c7f13534d6f0 [file] [log] [blame]
Kevin O'Connor03f630e2009-03-26 20:45:36 -04001#!/bin/sh
Kevin O'Connor84452f12009-03-27 00:07:07 -04002# Script to test if gcc "-fwhole-program" works properly.
Kevin O'Connor03f630e2009-03-26 20:45:36 -04003
4mkdir -p out
5TMPFILE1=out/tmp_testcompile1.c
6TMPFILE1o=out/tmp_testcompile1.o
Kevin O'Connor626416e2011-05-21 21:05:04 -04007TMPFILE1_ld=out/tmp_testcompile1.lds
Kevin O'Connor03f630e2009-03-26 20:45:36 -04008TMPFILE2=out/tmp_testcompile2.c
9TMPFILE2o=out/tmp_testcompile2.o
10TMPFILE3o=out/tmp_testcompile3.o
11
Kevin O'Connor626416e2011-05-21 21:05:04 -040012# Test if ld's alignment handling is correct. This is a known problem
13# with the linker that ships with Ubuntu 11.04.
14cat - > $TMPFILE1 <<EOF
15const char v1[] __attribute__((section(".text.v1"))) = "0123456789";
16const char v2[] __attribute__((section(".text.v2"))) = "0123456789";
17EOF
18cat - > $TMPFILE1_ld <<EOF
19SECTIONS
20{
21 .mysection 0x88f0 : {
22. = 0x10 ;
23*(.text.v1)
24. = 0x20 ;
25*(.text.v2)
26. = 0x30 ;
27 }
28}
29EOF
30$CC -O -g -c $TMPFILE1 -o $TMPFILE1o > /dev/null 2>&1
31$LD -T $TMPFILE1_ld $TMPFILE1o -o $TMPFILE2o > /dev/null 2>&1
32if [ $? -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
42fi
43
Kevin O'Connorab233da2009-06-30 21:13:44 -040044# Test for "-fwhole-program". Older versions of gcc (pre v4.1) don't
45# support the whole-program optimization - detect that.
Kevin O'Connor27876802009-03-27 00:12:00 -040046$CC -fwhole-program -S -o /dev/null -xc /dev/null > /dev/null 2>&1
Kevin O'Connor03f630e2009-03-26 20:45:36 -040047if [ $? -ne 0 ]; then
Kevin O'Connor942d4952009-06-10 22:44:06 -040048 echo " Working around no -fwhole-program" > /dev/fd/2
Kevin O'Connor0942e7f2009-06-15 22:27:01 -040049 echo 2
Kevin O'Connor942d4952009-06-10 22:44:06 -040050 exit 0
Kevin O'Connor03f630e2009-03-26 20:45:36 -040051fi
52
Kevin O'Connorab233da2009-06-30 21:13:44 -040053# 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'Connor03f630e2009-03-26 20:45:36 -040058cat - > $TMPFILE1 <<EOF
59void __attribute__((externally_visible)) t1() { }
Kevin O'Connora68287f2009-06-26 18:55:50 -040060extern unsigned char v1;
Kevin O'Connor0942e7f2009-06-15 22:27:01 -040061unsigned char v1 __attribute__((section(".data16.foo.19"))) __attribute__((externally_visible));
Kevin O'Connor03f630e2009-03-26 20:45:36 -040062EOF
Kevin O'Connor84452f12009-03-27 00:07:07 -040063$CC -Os -c -fwhole-program $TMPFILE1 -o $TMPFILE1o > /dev/null 2>&1
Kevin O'Connor03f630e2009-03-26 20:45:36 -040064cat - > $TMPFILE2 <<EOF
65void t1();
Kevin O'Connor0942e7f2009-06-15 22:27:01 -040066extern unsigned char v1;
67int __attribute__((externally_visible)) main() { t1(); return v1; }
Kevin O'Connor03f630e2009-03-26 20:45:36 -040068EOF
Kevin O'Connor84452f12009-03-27 00:07:07 -040069$CC -Os -c -fwhole-program $TMPFILE2 -o $TMPFILE2o > /dev/null 2>&1
Kevin O'Connorff82b242009-04-19 21:30:48 -040070$CC -nostdlib -Os $TMPFILE1o $TMPFILE2o -o $TMPFILE3o > /dev/null 2>&1
Kevin O'Connor03f630e2009-03-26 20:45:36 -040071if [ $? -ne 0 ]; then
Kevin O'Connor0942e7f2009-06-15 22:27:01 -040072 echo " Working around non-functional -fwhole-program" > /dev/fd/2
73 echo 2
74 exit 0
Kevin O'Connor03f630e2009-03-26 20:45:36 -040075fi
76
Kevin O'Connorab233da2009-06-30 21:13:44 -040077# 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'Connor3133e382009-11-20 09:19:28 -050079# 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'Connor03f630e2009-03-26 20:45:36 -040082cat - > $TMPFILE1 <<EOF
Kevin O'Connor3133e382009-11-20 09:19:28 -050083// Look for anonymous union/struct failure
Kevin O'Connor03f630e2009-03-26 20:45:36 -040084struct ts { union { int u1; struct { int u2; }; }; };
Kevin O'Connor3133e382009-11-20 09:19:28 -050085void func1(struct ts *r);
86
87// Look for global variable failure.
88struct s1_s { int v; } g1;
89void __attribute__((externally_visible)) func2() {
90 struct s1_s *l1 = &g1;
Kevin O'Connor89189892010-01-03 15:12:00 -050091 l1->v=0;
Kevin O'Connor3133e382009-11-20 09:19:28 -050092}
Kevin O'Connor03f630e2009-03-26 20:45:36 -040093EOF
Kevin O'Connor3133e382009-11-20 09:19:28 -050094cat - > $TMPFILE2 <<EOF
95struct ts { union { int u1; struct { int u2; }; }; };
96void func1(struct ts *r);
97
98extern struct s1_s g1;
99void func3() {
100 &g1;
101}
102EOF
103$CC -O -g -fwhole-program -combine -c $TMPFILE1 $TMPFILE2 -o $TMPFILE1o > /dev/null 2>&1
Kevin O'Connor03f630e2009-03-26 20:45:36 -0400104if [ $? -eq 0 ]; then
Kevin O'Connor03f630e2009-03-26 20:45:36 -0400105 echo 0
106else
Kevin O'Connor4d863fe2009-06-15 22:45:00 -0400107 echo " Working around non-functional -combine" > /dev/fd/2
Kevin O'Connor03f630e2009-03-26 20:45:36 -0400108 echo 1
109fi
110
Kevin O'Connor3133e382009-11-20 09:19:28 -0500111# 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'Connorab233da2009-06-30 21:13:44 -0400117# 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'Connor626416e2011-05-21 21:05:04 -0400121rm -f $TMPFILE1 $TMPFILE1o $TMPFILE1_ld $TMPFILE2 $TMPFILE2o $TMPFILE3o