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