ld relro
[platform/upstream/binutils.git] / ld / testsuite / ld-bootstrap / bootstrap.exp
1 # Expect script for LD Bootstrap Tests
2 #   Copyright (C) 1993-2014 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 # Written by Jeffrey Wheat (cassidy@cygnus.com)
22 # Rewritten by Ian Lance Taylor (ian@cygnus.com)
23 #
24
25 # Make sure that ld can bootstrap itself.
26
27 # This test can only be run if ld generates native executables.
28 if ![isnative] {
29     return
30 }
31
32 # Determine if plugin support is present.
33 remote_exec host "$nm --help" "" "/dev/null" "plugin-support"
34 set tmp [file_contents "plugin-support"]
35 regexp ".*\(--plugin\).*\n" $tmp foo plugins
36 if [info exists plugins] then {
37     set plugins "yes"
38 } else {
39     set plugins "no"
40 }
41
42 # LD can have plugin support even if BFD does not.
43 if [check_plugin_api_available] {
44     set plugins "yes"
45 }
46
47 # Bootstrap ld.  First link the object files together using -r, in
48 # order to test -r.  Then link the result into an executable, ld1, to
49 # really test -r.  Use ld1 to link a fresh ld, ld2.  Use ld2 to link a
50 # new ld, ld3.  ld2 and ld3 should be identical.
51 set test_flags {"" "strip" "--static" "--traditional-format"
52                 "--no-keep-memory" "--relax"}
53 if { [istarget "powerpc-*-*"] } {
54     lappend test_flags "--ppc476-workaround"
55 }
56
57 foreach flags $test_flags {
58     set do_strip "no"
59     if {"$flags" == "strip"} { 
60         set testname "bootstrap with $flags"
61         set flags ""
62         set do_strip "yes"
63     } else { if {"$flags" != ""} { 
64         set testname "bootstrap with $flags"
65     } else {
66         set testname "bootstrap"
67     }}
68
69     # --static is meaningless and --relax is incompatible with -r.
70     set partial_flags "$flags"
71     if { "$flags" == "--static" || "$flags" == "--relax" } {
72         set partial_flags ""
73     }
74
75     if { $partial_flags == "--ppc476-workaround" } {
76         append partial_flags " -T $srcdir/$subdir/ppc476.t"
77     }
78
79     # This test can only be run if we have the ld build directory,
80     # since we need the object files.
81     set ldexe $ld
82     set ldparm [string first " " $ld]
83     if { $ldparm > 0 } then {
84         set ldexe [string range $ld 0 $ldparm]
85     }
86     if {$ldexe != "$objdir/ld-new"} {
87         untested $testname
88         continue
89     }
90
91     # Plugin support requires linking with a dynamic library which
92     # means that these tests will fail.
93     if { $flags == "--static" && $plugins == "yes" } then {
94         untested $testname
95         continue
96     }
97
98     # If we only have a shared libbfd, we probably can't run the
99     # --static test.
100     if { $flags == "--static" && ! [string match "*libbfd.a*" $BFDLIB] } then {
101         untested $testname
102         continue
103     }
104
105     if ![ld_relocate $ld tmpdir/ld-partial.o "$partial_flags $OFILES"] {
106         fail $testname
107         continue
108     }
109
110     # On AIX, you need to specify an import list when using --static.
111     # You only want the import list when creating the final
112     # executable.
113     if { [istarget "*-*-aix*"] 
114          && ![istarget "ia64-*-aix*"]} {
115         if {"$flags" == "--static"} {
116             set flags "--static -bI:/lib/syscalls.exp"
117         }
118     }
119
120     # On Cygwin, -lintl may require -liconv when linking statically.
121     set extralibs ""
122     if { [istarget "*-*-cygwin*"]} {
123         if {"$flags" == "--static"} {
124             set extralibs "-liconv"
125         }
126     }
127
128     # Plugin support requires linking with libdl.
129     if { $plugins == "yes" } {
130         if { ![istarget "*-*-freebsd*"]} {
131             set extralibs "$extralibs -ldl"
132         }
133     }
134
135     # On Irix 5, linking with --static only works if all the files are
136     # compiled using -non_shared.
137     if {"$flags" == "--static"} {
138         setup_xfail "mips*-*-irix5*"
139     }
140
141     if ![ld_link $ld tmpdir/ld1 "$flags tmpdir/ld-partial.o $BFDLIB $LIBIBERTY $extralibs"] {
142         fail $testname
143         continue
144     }
145
146     if {"$do_strip" == "yes"} { 
147         verbose -log "$strip tmpdir/ld1"     
148         catch "exec $strip tmpdir/ld1" exec_output
149         if ![string match "" $exec_output] then {
150             verbose -log "$exec_output"
151             fail $testname
152             continue
153         }
154     }
155
156     if ![ld_link tmpdir/ld1 tmpdir/ld2 "$flags $OFILES $BFDLIB $LIBIBERTY $extralibs"] {
157         fail $testname
158         continue
159     }
160
161     if ![ld_link tmpdir/ld2 tmpdir/ld3 "$flags $OFILES $BFDLIB $LIBIBERTY $extralibs"] {
162         fail $testname
163         continue
164     }
165
166     if {"$flags" == "--static"} {
167         if { [istarget ia64-*-elf*]
168              || [istarget ia64-*-linux*] } {
169             # On ia64, tmpdir/ld2 != tmpdir/ld3 is normal since they are
170             # generated by different linkers, tmpdir/ld1 and tmpdir/ld2.
171             # So we rebuild tmpdir/ld2 with tmpdir/ld3.
172             if ![ld_link tmpdir/ld3 tmpdir/ld2 "$flags $OFILES $BFDLIB $LIBIBERTY $extralibs"] {
173                 fail $testname
174                 continue
175             }
176         }
177     } else {
178         if { [istarget mips*-*-linux*] } {
179             # On Linux/mips, tmpdir/ld2 != tmpdir/ld3 is normal since
180             # they are generated by different linkers, tmpdir/ld1 and
181             # tmpdir/ld2. So we rebuild tmpdir/ld2 with tmpdir/ld3.
182             if ![ld_link tmpdir/ld3 tmpdir/ld2 "$flags $OFILES $BFDLIB $LIBIBERTY $extralibs"] {
183                 fail $testname
184                 continue
185             }
186         }
187     }
188
189     if {[istarget "*-*-pe"]
190         || [istarget "*-*-wince"]
191         || [istarget "*-*-cygwin*"]
192         || [istarget "*-*-winnt*"]
193         || [istarget "*-*-mingw*"]
194         || [istarget "*-*-interix*"]
195         || [istarget "*-*-beospe*"]
196         || [istarget "*-*-netbsdpe*"]} {
197         # Trim off the date present in PE binaries by only looking
198         #   at the ends of the files
199         # Although this works, a way to set the date would be better.
200         # Removing or zeroing the date stamp in the binary produced by
201         # the linker is not possible as it is required by the target OS.
202         set do_compare [string map {16 220 f1 tmpdir/ld2 f2 tmpdir/ld3 tmp-foo1 tmpdir/ld2tail tmp-foo2 tmpdir/ld3tail} $DO_COMPARE]
203         send_log "$do_compare\n"
204         verbose  "$do_compare"
205         catch "exec sh -c [list $do_compare]" exec_output
206     } else {
207         send_log "cmp tmpdir/ld2 tmpdir/ld3\n"
208         verbose  "cmp tmpdir/ld2 tmpdir/ld3"
209         catch "exec cmp tmpdir/ld2 tmpdir/ld3" exec_output
210     }
211     set exec_output [prune_warnings $exec_output]
212
213     if [string match "" $exec_output] then {
214         pass $testname
215     } else {
216         send_log "$exec_output\n"
217         verbose "$exec_output" 1
218
219         fail $testname
220     }
221 }
222
223 catch "exec rm -f tmpdir/ld-partial.o tmpdir/ld1 tmpdir/ld2 tmpdir/ld3" status
224 catch "exec rm -f tmpdir/ld2tail tmpdir/ld3tail" status