Use better test for usable compiler in ld testsuite.
[external/binutils.git] / ld / testsuite / ld-gc / gc.exp
1 # Expect script for ld-gc tests
2 #   Copyright (C) 2008-2019 Free Software Foundation, Inc.
3 #
4 # This file is part of the GNU Binutils.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program 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
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 # MA 02110-1301, USA.
20
21 # These tests require --gc-sections
22 if ![check_gc_sections_available] {
23     return
24 }
25
26 set cflags "-ffunction-sections -fdata-sections"
27 set objfile "tmpdir/gc.o"
28
29 # Add $NOPIE_CFLAGS and $NOPIE_LDFLAGS if non-PIE is required.
30 global NOPIE_CFLAGS NOPIE_LDFLAGS
31
32 if [istarget powerpc64*-*-*] {
33     # otherwise with -mcmodel=medium gcc we get XPASSes.
34     set cflags "$cflags -mminimal-toc"
35 }
36
37 if { [istarget m681*-*-*] || [istarget m68hc1*-*-*] } {
38     # Otherwise tests FAIL due to _.frame
39     set cflags "$cflags -fomit-frame-pointer -mshort"
40 }
41
42 if { [check_compiler_available] } {
43     ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/gc.c $objfile
44 }
45
46 proc test_gc { testname filename linker ldflags} {
47     global nm
48     global srcdir
49     global subdir
50     global nm_output
51     global objfile
52
53     if ![file readable $objfile ] {
54         untested $testname
55         return
56     }
57
58     set outfile "tmpdir/$filename"
59     set options "-L$srcdir/$subdir"
60     append options " " $ldflags " " [ld_link_defsyms] " " $objfile
61
62     if ![ld_link $linker $outfile $options] {
63         fail $testname
64         return
65     }
66     if ![ld_nm $nm "" $outfile] {
67         unresolved $testname
68         return
69     }
70     if {![info exists nm_output(used_func)] \
71             || ![info exists nm_output(used_var)]} {
72         send_log "used sections do not exist\n"
73         verbose "used sections do not exist"
74         fail $testname
75         return
76     }
77     #ppc64_elf_gc_mark_hook needs to be taught how to look through
78     #the .toc section to properly mark variable sections for gc.
79     setup_xfail "powerpc64*-*-*"
80     if {[info exists nm_output(unused_func)] \
81             || [info exists nm_output(unused_var)]} {
82         send_log "unused section still here\n"
83         verbose "unused section still here"
84         fail $testname
85         return
86     }
87     pass $testname
88 }
89
90 test_gc "Check --gc-section" "gcexe" $ld "--gc-sections -e main"
91 test_gc "Check --gc-section/-q" "gcrexe" $ld "--gc-sections -q -e main"
92 test_gc "Check --gc-section/-r/-e" "gcrel" $ld "-r --gc-sections -e main"
93 test_gc "Check --gc-section/-r/-u" "gcrel" $ld "-r --gc-sections -u used_func"
94
95 run_dump_test "noent"
96 run_dump_test "abi-note"
97 run_dump_test "start"
98 run_dump_test "stop"
99 run_dump_test "pr19167"
100 if { [is_elf_format] } then {
101     run_dump_test "all-debug-sections"
102     run_dump_test "pr20882"
103 }
104
105 if { [is_elf_format] && [check_shared_lib_support] } then {
106     set gasopt ""
107     if { [istarget tic6x-*] } then {
108         set gasopt "-mpic -mpid=near"
109     }
110     # These targets do not default to linking with shared libraries.
111     set old_ldflags $LDFLAGS
112     if { [istarget "mips*vr4100*-*-elf*"] \
113          || [istarget "mips*vr4300*-*-elf*"] \
114          || [istarget "mips*vr5000*-*-elf*"] } {
115         append LDFLAGS " -call_shared"
116     }
117     if {![ld_assemble_flags $as $gasopt $srcdir/$subdir/libpersonality.s tmpdir/libpersonality.o]
118         || ![ld_link $ld tmpdir/libpersonality.so "-shared tmpdir/libpersonality.o"] } then {
119         fail libpersonality
120     } else {
121         run_dump_test "personality"
122     }
123     run_dump_test "pr18223"
124     if {![ld_assemble_flags $as $gasopt $srcdir/$subdir/pr20022a.s tmpdir/pr20022a.o]
125         || ![ld_link $ld tmpdir/pr20022.so "-shared --gc-sections tmpdir/pr20022a.o"] } then {
126         fail pr20022
127     } else {
128         run_dump_test "pr20022"
129     }
130     set LDFLAGS $old_ldflags
131 }
132
133 if { [check_compiler_available] } {
134     if { [istarget "*-*-linux*"]
135          || [istarget "*-*-nacl*"]
136          || [istarget "*-*-gnu*"] } {
137         ld_compile "$CC -fPIC $CFLAGS $cflags" $srcdir/$subdir/pr11218-1.c tmpdir/pr11218-1.o
138         ld_link $ld tmpdir/pr11218-1.so "-shared tmpdir/pr11218-1.o"
139         ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/pr11218-2.c tmpdir/pr11218-2.o
140         run_dump_test "pr11218"
141     }
142 }
143
144 if { [check_compiler_available] } {
145         ld_compile "$CC $CFLAGS $cflags" $srcdir/$subdir/pr13683.c tmpdir/pr13683.o
146         run_dump_test "pr13683"
147 }
148
149 if { [check_compiler_available] } {
150     ld_compile "$CC $CFLAGS $cflags $NOPIE_CFLAGS" $srcdir/$subdir/pr14265.c tmpdir/pr14265.o
151     run_dump_test "pr14265"
152 }
153
154 if { [check_compiler_available] } {
155     ld_compile "$CC $CFLAGS $cflags -O0" $srcdir/$subdir/pr19161-1.c tmpdir/pr19161-1.o
156     ld_compile "$CC $CFLAGS $cflags -O0" $srcdir/$subdir/pr19161-2.c tmpdir/pr19161-2.o
157     run_dump_test "pr19161"
158 }
159
160 if { [is_elf_format] && [check_shared_lib_support] \
161      && [check_compiler_available] } {
162     run_cc_link_tests [list \
163         [list \
164             "Build libpr20306.so" \
165             "-shared" \
166             "-fPIC" \
167             {pr20306.c} \
168             {} \
169             "libpr20306.so" \
170         ] \
171     ]
172     run_dump_test "pr20306"
173 }
174
175 if { [is_elf_format] } then {
176     run_dump_test "skip-map-discarded"
177 }