acdad8db22ae09191200849cd349905ab83690fa
[external/binutils.git] / ld / testsuite / ld-elf / elf.exp
1 # Expect script for various ELF tests.
2 #   Copyright (C) 2002-2017 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
22 # Exclude non-ELF targets.
23
24 if ![is_elf_format] {
25     return
26 }
27
28 set old_ldflags $LDFLAGS
29 if { [istarget spu*-*-*] } {
30     set LDFLAGS "$LDFLAGS --local-store 0:0"
31 }
32
33 # hpux .comm differs from everyone else
34 set hpux ""
35 set old_asflags $ASFLAGS
36 if [istarget "*-*-hpux*"] {
37     set hpux "--defsym HPUX=1"
38     set ASFLAGS "$ASFLAGS --defsym HPUX=1"
39 }
40
41 if { [istarget alpha*-*-* ] } {
42     # The compress1 test is written expecting 32-bit addresses; force the
43     # executable down into the low address space to match.
44     # ??? How can we adjust just the one testcase?
45     set LDFLAGS "$LDFLAGS -Ttext-segment 0x1000000"
46 }
47
48 if { [istarget "*-*-nacl*"] } {
49     # The eh[1-4] cases are written to expect ELFCLASS64 layout on x86-64.
50     # But the target default is ELFCLASS32.  So the cases explicitly use
51     # -melf_x86_64 to select that, but NaCl needs a different emulation name.
52     set options_regsub(ld) {-melf_x86_64 -melf_x86_64_nacl}
53 }
54
55 if { [istarget "*-*-solaris*"] } {
56     # Same for Solaris
57     set options_regsub(ld) {-melf_x86_64 -melf_x86_64_sol2}
58 }
59
60 if { [is_remote host] } then {
61     remote_download host merge.ld
62 }
63
64 run_ld_link_tests [list \
65     [list "Build symbol3.a" \
66         "" "" $hpux \
67         {symbol3.s} {} "symbol3.a" ] \
68     [list "Build symbol3w.a" \
69         "" "" "" \
70         {symbol3w.s} {} "symbol3w.a" ] \
71 ]
72
73 # Targets that use _bfd_generic_link_add_symbols won't pass pr21703 tests
74 run_ld_link_tests {
75       {"PR ld/21703"
76         "--allow-multiple-definition tmpdir/pr21703-1.o tmpdir/pr21703-2.o" "" "" \
77         {pr21703-1.s pr21703-2.s} {{readelf {-s} pr21703.sd}} "pr21703" }
78       {"PR ld/21703 -r"
79         "-r --allow-multiple-definition tmpdir/pr21703-3.o tmpdir/pr21703-4.o" "" "" \
80         {pr21703-3.s pr21703-4.s} {{readelf {-s} pr21703-r.sd}} "pr21703.o" }
81 } "d30v-*-*" "dlx-*-*" "pj-*-*"
82
83 # Only run these tests on targets that support creating shared libraries.
84 if { [check_shared_lib_support] } then {
85     run_ld_link_tests {
86         {"Build pr14170a.o" "" "" "" {pr14170a.s} {} "pr14170.a" }
87     }
88     run_ld_link_tests {
89         {"Build shared library for pr14170"
90             "-shared" "" "" "pr14170b.s" {} "pr14170.so" }
91     } "tic6x-*-*"
92
93     # bfin does not currently support copy relocs.
94     run_ld_link_tests [list \
95         [list "PR ld/14170" \
96             "--no-dynamic-linker tmpdir/pr14170a.o tmpdir/pr14170.so" "" $hpux \
97              {pr14170c.s} { } "pr14170" ] \
98     ] "bfin-*-*"
99
100     # Targets that use _bfd_generic_link_add_symbols won't pass pr21703 tests
101     # Not will tic6x with PIC/PID warning, or hppa64 with dot-symbols
102     run_ld_link_tests {
103         {"PR ld/21703 shared"
104          "-shared --allow-multiple-definition --version-script pr21703.ver tmpdir/pr21703-3.o tmpdir/pr21703-4.o" "" "" \
105          {pr21703-3.s pr21703-4.s} {{readelf {--dyn-syms} pr21703-shared.sd}} "pr21703.so" }
106     } "d30v-*-*" "dlx-*-*" "pj-*-*" "tic6x-*-*" "hppa64-*-*"
107
108     # This target requires extra GAS options when building non-PIC code
109     # for linking with shared libraries.
110     set AFLAGS_NONPIC ""
111     if [istarget "mips*-*-*"] {
112         append AFLAGS_NONPIC " -call_nonpic"
113     }
114
115     # Run a test to check linking a shared library with a broken linker
116     # script that accidentally marks dynamic sections as notes.  The
117     # resulting executable is not expected to work, but the linker
118     # should not seg-fault whilst creating the binary.
119     # Note: setup_xfail before run_ld_link_tests xfails only the first test.
120     setup_xfail "tic6x-*-*"
121     run_ld_link_tests {
122         {"Build shared library for broken linker scrip test"
123             "-shared --hash-style=sysv" "" "" "note-3.s" {} "note-3.so" }
124         {"Link using broken linker script"
125             "--script note-3.t tmpdir/note-3.so" "" "" ""
126             { { ld "note-3.l" } }
127             "a.out" }
128     }
129
130     setup_xfail "tic6x-*-*"
131     run_ld_link_tests {
132         {"Build pr17068.so"
133             "-shared" "" ""
134             {pr17068d.s} {} "pr17068.so"}
135         {"Build pr17068a.a"
136             "" "" ""
137             {pr17068a.s pr17068c.s pr17068ez.s} {} "pr17068a.a"}
138         {"Build pr17068b.a"
139             "" "" ""
140             {pr17068b.s pr17068e.s} {} "pr17068b.a"}
141     }
142
143     # bfin does not currently support copy relocs.
144     run_ld_link_tests {
145         {"pr17068 link --as-needed lib in group"
146             "--as-needed --no-dynamic-linker" "--start-group tmpdir/pr17068a.a tmpdir/pr17068.so tmpdir/pr17068b.a --end-group" ""
147             {start.s pr17068.s} {} "pr17068"}
148     } "bfin-*-*"
149
150     # xfail on tic6x due to non-PIC/non-PID warnings
151     # Fails on MIPS because ABI trickery means that a NULL reloc is emitted.
152     # Fails on bfin because relocations are not created.
153     run_ld_link_tests {
154         {"-Bsymbolic-functions"
155             "-shared -Bsymbolic-functions" "" ""
156             {symbolic-func.s} {{readelf {-r --wide} symbolic-func.r}}
157             "symbolic-func.so"}
158     } "tic6x-*-*" "mips*-*-*" "bfin-*-*"
159
160     # xfail on tic6x due to non-PIC/non-PID warnings
161     run_ld_link_tests {
162         {"Build pr20995.so"
163             "-shared" "" ""
164             {pr20995b.s} {} "pr20995.so"}
165     } "tic6x-*-*"
166
167     # xfail on arm*-*-eabi*.  The list can be enlarged to those targets that
168     # don't support GNU_RELRO.  For more details, please see discussions at:
169     #   https://sourceware.org/ml/binutils/2017-01/msg00441.html
170     run_ld_link_tests {
171         {"Build pr20995-2.so"
172             "-shared -z relro" "" ""
173             {pr20995c.s} {{readelf {-l --wide} pr20995-2so.r}} "pr20995-2.so"}
174     } "tic6x-*-*" "arm*-*-eabi*" "hppa*64*-*-hpux*"
175
176     # These targets don't copy dynamic variables into .bss.
177     setup_xfail "alpha-*-*" "bfin-*-*" "ia64-*-*" "xtensa-*-*"
178     # or don't have .data.rel.ro
179     setup_xfail "hppa*64*-*-hpux*"
180     run_ld_link_tests [list \
181         [list \
182             "pr20995" \
183             "" "tmpdir/pr20995.so" "$AFLAGS_NONPIC" \
184             {pr20995a.s} {{readelf {-S --wide} pr20995.r}} "pr20995"]]
185
186     # xfail on arm*-*-eabi* is particularly because of no support of GNU_RELRO.
187     # Please see the link above for details.
188     setup_xfail "alpha-*-*" "bfin-*-*" "ia64-*-*" "xtensa-*-*" "arm*-*-eabi*"
189     setup_xfail "hppa*64*-*-hpux*"
190     run_ld_link_tests [list \
191         [list \
192             "pr20995-2" \
193             "" "tmpdir/pr20995-2.so" "$AFLAGS_NONPIC" \
194             {pr20995a.s} {{readelf {-S --wide} pr20995.r}} "pr20995-2"]]
195
196     setup_xfail "tic6x-*-*"
197     run_ld_link_tests {
198         {"Build pr22374 shared library"
199             "-shared" "" "" "pr22374b.s" {} "pr22374.so" }
200         {"pr22374 function pointer initialization"
201             "" "tmpdir/pr22374.so" "" "pr22374a.s"
202             { {readelf {--wide -r --dyn-syms} "pr22374-1.r"}
203               {readelf {--wide -r} "pr22374-2.r"} }
204             "pr22374" }
205     }
206 }
207
208 if [is_underscore_target] {
209     set ASFLAGS "$ASFLAGS --defsym UNDERSCORE=1"
210 }
211
212 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
213 foreach t $test_list {
214     # We need to strip the ".d", but can leave the dirname.
215     verbose [file rootname $t]
216     run_dump_test [file rootname $t]
217 }
218
219 # Targets using the generic linker backend don't support generating
220 # an import library.
221 set xfail_implib ""
222 if [is_generic_elf] {
223     set xfail_implib "*-*-*"
224 }
225
226 # Check that the --out-implib option work correctly.
227 run_ld_link_tests [list \
228     [list "Generate empty import library" \
229         "--out-implib=tmpdir/implib.lib" "" \
230         [concat "--defsym NO_GLOBAL=1" $hpux] \
231         {implib.s} \
232         {{ld empty-implib.out}} \
233         "implib" ] \
234     [list "Generate import library" \
235         "--out-implib=tmpdir/implib.lib" "" \
236         $hpux \
237         {implib.s} \
238         {{readelf {-s tmpdir/implib.lib} implib.rd}} \
239         "implib" ] \
240 ] $xfail_implib
241
242 if { [istarget *-*-linux*]
243      || [istarget *-*-nacl*]
244      || [istarget *-*-gnu*] } {
245     run_ld_link_tests {
246         {"Weak symbols in dynamic objects 1 (support)"
247             "-shared" "" "" {weak-dyn-1a.s}
248             {}
249             "libweakdyn1a.so"}
250         {"Weak symbols in dynamic objects 1 (main test)"
251             "-shared tmpdir/libweakdyn1a.so -Tweak-dyn-1.ld" "" "" {weak-dyn-1b.s}
252             {{readelf {--relocs --wide} weak-dyn-1.rd}}
253             "libweakdyn1b.so"}
254     }
255 }
256
257 #v850 gas complains about .tbss.var section attributes.
258 if { [check_gc_sections_available] && ![istarget "v850-*-*"] } {
259     run_ld_link_tests {
260         {"--gc-sections on tls variable"
261             "--gc-section" "" "" {tls_gc.s} {} "tls_gc"}
262     }
263 }
264
265 if { [istarget *-*-*linux*]
266      || [istarget *-*-nacl*]
267      || [istarget *-*-gnu*] } {
268     run_ld_link_tests {
269         {"stack exec" "-z execstack" "" "" {stack.s}
270             {{readelf {-Wl} stack-exec.rd}} "stack-exec.exe"}
271         {"stack size" "-z stack-size=0x123400" "" "" {stack.s}
272             {{readelf {-Wl} stack-size.rd}} "stack-size.exe"}
273     }
274 }
275
276 set LDFLAGS $old_ldflags
277 set ASFLAGS $old_asflags
278
279 # Check to see if the C compiler works
280 if { [which $CC] == 0 } {
281     return
282 }
283
284 if [check_gc_sections_available] {
285     run_cc_link_tests {
286         {"PR ld/13195" "-Wl,--gc-sections" ""
287          {pr13195.c} {} "pr13195"}
288     }
289 }
290
291 set array_tests {
292     {"preinit array" "" ""
293         {preinit.c} "preinit" "preinit.out"}
294     {"init array" "" ""
295         {init.c} "init" "init.out"}
296     {"fini array" "" ""
297         {fini.c} "fini" "fini.out"}
298     {"init array mixed" "" ""
299         {init-mixed.c} "init-mixed" "init-mixed.out" "-I."}
300 }
301 set array_tests_pie {
302     {"PIE preinit array" "-pie" ""
303         {preinit.c} "preinit" "preinit.out" "-fPIE"}
304     {"PIE init array" "-pie" ""
305         {init.c} "init" "init.out" "-fPIE"}
306     {"PIE fini array" "-pie" ""
307         {fini.c} "fini" "fini.out" "-fPIE"}
308     {"PIE init array mixed" "-pie" ""
309         {init-mixed.c} "init-mixed" "init-mixed.out" "-I. -fPIE"}
310     {"PIE PR ld/14525" "-pie" ""
311         {pr14525.c} "pr14525" "pr14525.out" "-fPIE"}
312 }
313 set array_tests_static {
314     {"static preinit array" "-static" ""
315         {preinit.c} "preinit" "preinit.out"}
316     {"static init array" "-static" ""
317         {init.c} "init" "init.out"}
318     {"static fini array" "-static" ""
319         {fini.c} "fini" "fini.out"}
320     {"static init array mixed" "-static" ""
321         {init-mixed.c} "init-mixed" "init-mixed.out" "-I."}
322 }
323
324 # NetBSD ELF systems do not currently support the .*_array sections.
325 set xfails "*-*-netbsdelf*"
326 run_ld_link_exec_tests $array_tests $xfails
327
328 if { [istarget *-*-linux*]
329      || [istarget *-*-nacl*]
330      || [istarget *-*-gnu*] } {
331     run_ld_link_exec_tests $array_tests_pie $xfails
332
333     run_ld_link_exec_tests [list \
334         [list \
335             "Run mbind2a" \
336             "$NOPIE_LDFLAGS -Wl,-z,common-page-size=0x4000" \
337             "" \
338             { mbind2a.s mbind2b.c } \
339             "mbind2a" \
340             "pass.out" \
341             "-O2 -I../bfd" \
342         ] \
343         [list \
344             "Run mbind2b" \
345             "-static -Wl,-z,common-page-size=0x4000" \
346             "" \
347             { mbind2a.s mbind2b.c } \
348             "mbind2b" \
349             "pass.out" \
350             "-O2 -I../bfd" \
351         ] \
352     ]
353 }
354
355 # <http://www.gnu.org/software/hurd/open_issues/binutils.html#static>
356 # Be cautious to not XFAIL for *-*-linux-gnu*, *-*-kfreebsd-gnu*, etc.
357 switch -regexp $target_triplet {
358     ^\[^-\]*-\[^-\]*-gnu.*$ {
359         set xfails "*-*-*"
360     }
361 }
362 run_ld_link_exec_tests $array_tests_static $xfails
363
364 catch "exec rm -f tmpdir/preinit tmpdir/init tmpdir/fini tmpdir/init-mixed" status