bfd:
[external/binutils.git] / ld / testsuite / ld-selective / selective.exp
1 # Expect script for LD selective linking tests
2 #   Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007
3 #   Free Software Foundation, Inc.
4 #
5 # This file is part of the GNU Binutils.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 # MA 02110-1301, USA.
21 #
22 # Written by Catherine Moore (clm@cygnus.com)
23 # Make sure that constructors are handled correctly.
24
25 # Only ELF based ports support selective linking
26 if ![is_elf_format] {
27     return
28 }
29
30 # Alpha and IA64 do not support selective linking
31 if {[istarget "alpha*-*-*"] || [istarget "ia64-*-*"]} {
32     return
33 }
34
35 # List contains test-items with three items followed by four lists:
36 # 1:name 2:test-type (CC or C++; add as needed) 3:filename 4:ld-flags
37 # 5:must-have-symbols 6:must-not-have-symbols 7:xfail-targets.
38 #
39 # If a must(-not)-have symbol is a list, then that list must have two
40 # items; the symbol name and a value the symbol must (not) have.
41 #
42 # Note: ld_nm trims leading `_' from _start
43 #
44 # FIXME: Instead of table, read settings from each source-file.
45 set seltests {
46   {selective1 C   1.c  {}       {}    {dropme1 dropme2} {}}
47   {selective2 C   2.c  {}       {}    {foo} {}}
48   {selective3 C   2.c  {-u foo} {foo} {{foo 0}} {}}
49   {selective4 C++ 3.cc {}       {start a A::foo() B::foo()} {A::bar()} {mips*-*}}
50   {selective5 C++ 4.cc {}       {start a A::bar()} {A::foo() B::foo()} {mips*-*}}
51   {selective6 C++ 5.cc {}       {start a A::bar()}
52     {A::foo() B::foo() dropme1() dropme2()} {*-*-*}}
53 }
54
55 set cflags "-w -O -ffunction-sections -fdata-sections"
56 set cxxflags "-fvtable-gc -fno-exceptions -fno-rtti"
57 set ldflags "--gc-sections -Bstatic"
58
59 if [istarget mips*-*] {
60     # MIPS16 doesn't support PIC code.
61     set cflags "-mno-abicalls $cflags"
62     # MIPS ELF uses __start by default, we override it.
63     set ldflags "-e _start $ldflags"
64 }
65
66 if [istarget sh64*-*-elf] {
67     # This is what gcc passes to ld by default, plus switch to the
68     # "usual" ELF _start (shelf32 normally uses just `start' for COFF
69     # compatibility)
70     set ldflags "-e _start -mshelf32 $ldflags"
71 }
72
73 # If we don't have g++ for the target, mark all tests as untested.
74 if { ![is_remote host] && [which $CXX] == 0 } {
75     foreach testitem $seltests {
76         untested "[lindex $testitem 0]"
77     }
78     return
79 }
80
81 foreach testitem $seltests {
82     set testname [lindex $testitem 0]
83     set testtype [lindex $testitem 1]
84     set testfile [lindex $testitem 2]
85     set objfile "tmpdir/[file rootname $testfile].o"
86     set ldfile "tmpdir/[file rootname $testfile].x"
87     set failed 0
88
89     set ldargs [lindex $testitem 3]
90     set mustsyms [lindex $testitem 4]
91     set mustnotsyms [lindex $testitem 5]
92     set xfails [lindex $testitem 6]
93
94     foreach xfail_target $xfails {
95         setup_xfail $xfail_target
96     }
97     setup_xfail "arc*-*" "d30v*-*" "dlx*-*" "i370*-*" "i860*-*"
98     setup_xfail "i960*-*" "mn10200-*" "or32-*" "pj-*"
99
100     # It's either C or C++ at the moment.
101     if { $testtype == "C++" } {
102         set testflags "$cflags $cxxflags"
103         if [string match "*gcc*" [lindex $CC 0]] {
104             # Starting with 3.4.0, -fvtable-gc is no longer supported and thus
105             # the functionality we try to test for cannot be expected to work.
106             set version [remote_exec host "$CC -dumpversion"]
107             set version [lindex $version 1]
108             if [regexp "^(\[1-9\]\[0-9\]+|\[4-9\]|3.(\[1-9\]\[0-9\]+|\[4-9\]))\\." $version] {
109                 setup_xfail {*-*-*}
110             }
111         }
112     } {
113         set testflags "$cflags"
114     }
115
116     # Note that we do not actually *use* CXX; we just add cxxflags for C++
117     # tests.  It might have been a buglet originally; now I think better
118     # leave as is.
119     if { ![ld_compile "$CC $testflags" $srcdir/$subdir/$testfile $objfile] } {
120         unresolved $testname
121         continue
122     }
123
124     # V850 targets need libgcc.a
125     if [istarget v850*-*-elf] {
126         set libgcc [remote_exec host "$CC -print-libgcc-file-name"]
127         set libgcc [lindex $libgcc 1]
128         regsub -all "\[\r\n\]" $libgcc "" libgcc
129         set objfile "$objfile $libgcc"
130     }
131
132     # ARM targets need libgcc.a in THUMB mode so that __call_via_r3 is provided
133     if {[istarget arm-*-*] || [istarget xscale-*-*]} {
134         set libgcc [remote_exec host "$CC -print-libgcc-file-name"]
135         set libgcc [lindex $libgcc 1]
136         regsub -all "\[\r\n\]" $libgcc "" libgcc
137         set objfile "$objfile $libgcc"
138     }
139
140     # HPPA linux targets need libgcc.a for millicode routines ($$dyncall).
141     if [istarget hppa*-*-linux*] {
142         set libgcc [remote_exec host "$CC -print-libgcc-file-name"]
143         set libgcc [lindex $libgcc 1]
144         regsub -all "\[\r\n\]" $libgcc "" libgcc
145         set objfile "$objfile $libgcc"
146     }
147
148     # m6811/m6812 code has references to soft registers.
149     if {[istarget m6811-*-*] || [istarget m6812-*-*]} {
150         set objfile "$objfile --defsym _.frame=0 --defsym _.d1=0"
151         set objfile "$objfile --defsym _.d2=0"
152     }
153
154     if ![ld_simple_link $ld $ldfile "$ldflags [join $ldargs] $objfile"] {
155         fail $testname
156         continue
157     }
158
159     if ![ld_nm $nm --demangle $ldfile] {
160         unresolved $testname
161         continue
162     }
163
164     # Must make V2 demangled names look like V3
165     foreach nm_output_key [array names nm_output] {
166         if [regsub \\(void\\) $nm_output_key () new_nm_output_key] {
167             set nm_output($new_nm_output_key) nm_output($nm_output_key)
168         }
169     }
170
171     # Check each mandated symbol and optionally mandated values.
172     foreach mustsym $mustsyms {
173         if { [llength [concat $mustsym]] == 1 } {
174             if { ![info exists nm_output($mustsym)] } {
175                 verbose -log "$testname: missing $mustsym"
176                 fail $testname
177                 set failed 1
178                 break
179             }
180         } {
181             set mustsymname [lindex $mustsym 0]
182             set mustsymvalue [lindex $mustsym 1]
183             if { ![info exists nm_output($mustsymname)] } {
184                 verbose -log "$testname: missing $mustsymname"
185                 fail $testname
186                 set failed 1
187                 break
188             } {
189                 if { $nm_output($mustsymname) != $mustsymvalue } {
190                     verbose -log "$testname: $mustsymname != $mustsymvalue"
191                     verbose -log "is instead $nm_output($mustsymname)"
192                     fail $testname
193                     set failed 1
194                     break
195                 }
196             }
197         }
198     }
199
200     if { $failed != 0 } {
201         continue
202     }
203
204     # Check each unwanted symbol, or that symbols do not have specific
205     # values.
206     foreach mustnotsym $mustnotsyms {
207         if { [llength [concat $mustnotsym]] == 1 } {
208             if { [info exists nm_output($mustnotsym)] } {
209                 verbose -log "$testname: $mustnotsym == $nm_output($mustnotsym)"
210                 fail $testname
211                 set failed 1
212                 break
213             }
214         } {
215             set mustnotsymname [lindex $mustnotsym 0]
216             set mustnotsymvalue [lindex $mustnotsym 1]
217             if { [info exists nm_output($mustnotsymname)] \
218                     && $nm_output($mustnotsymname) == $mustnotsymvalue} {
219                 verbose -log "$testname: $mustnotsymname == $mustnotsymvalue"
220                 fail $testname
221                 set failed 1
222                 break
223             }
224         }
225     }
226
227     if { $failed == 0 } {
228         pass $testname
229     }
230 }