8f24a75a74bc549617266d8b607e85c59bf3bcd2
[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 # Note - the output file from the second test (symbol3w.a) is
65 # used in the proc is_elf64 test below...
66 run_ld_link_tests [list \
67     [list "Build symbol3.a" \
68         "" "" $hpux \
69         {symbol3.s} {} "symbol3.a" ] \
70     [list "Build symbol3w.a" \
71         "" "" "" \
72         {symbol3w.s} {} "symbol3w.a" ] \
73 ]
74
75
76 # True if the object format is known to be 64-bit ELF.
77 #
78 proc is_elf64 { binary_file } {
79     global READELF
80     global READELFFLAGS
81
82     set readelf_size ""
83     catch "exec $READELF $READELFFLAGS -h $binary_file > readelf.out" got
84
85     if ![string match "" $got] then {
86         return 0
87     }
88
89     if { ![regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
90                [file_contents readelf.out] nil readelf_size] } {
91         verbose "FAILED to determine ELF size"
92         return 0
93     }
94
95     if { $readelf_size == "64" } {
96         return 1
97     }
98
99     return 0
100 }
101
102 if [is_elf64 tmpdir/symbol3w.a] {
103     set ASFLAGS "$ASFLAGS --defsym ALIGN=3"
104 } else {
105     set ASFLAGS "$ASFLAGS --defsym ALIGN=2"
106 }
107
108
109
110 # Targets that use _bfd_generic_link_add_symbols won't pass pr21703 tests
111 run_ld_link_tests {
112       {"PR ld/21703"
113         "--allow-multiple-definition tmpdir/pr21703-1.o tmpdir/pr21703-2.o" "" "" \
114         {pr21703-1.s pr21703-2.s} {{readelf {-s} pr21703.sd}} "pr21703" }
115       {"PR ld/21703 -r"
116         "-r --allow-multiple-definition tmpdir/pr21703-3.o tmpdir/pr21703-4.o" "" "" \
117         {pr21703-3.s pr21703-4.s} {{readelf {-s} pr21703-r.sd}} "pr21703.o" }
118 } "d30v-*-*" "dlx-*-*" "pj-*-*"
119
120 # Only run these tests on targets that support creating shared libraries.
121 if { [check_shared_lib_support] } then {
122     run_ld_link_tests {
123         {"Build pr14170a.o" "" "" "" {pr14170a.s} {} "pr14170.a" }
124     }
125     run_ld_link_tests {
126         {"Build shared library for pr14170"
127             "-shared" "" "" "pr14170b.s" {} "pr14170.so" }
128     } "tic6x-*-*"
129
130     # bfin does not currently support copy relocs.
131     run_ld_link_tests [list \
132         [list "PR ld/14170" \
133             "--no-dynamic-linker tmpdir/pr14170a.o tmpdir/pr14170.so" "" $hpux \
134              {pr14170c.s} { } "pr14170" ] \
135     ] "bfin-*-*"
136
137     # Targets that use _bfd_generic_link_add_symbols won't pass pr21703 tests
138     # Not will tic6x with PIC/PID warning, or hppa64 with dot-symbols
139     run_ld_link_tests {
140         {"PR ld/21703 shared"
141          "-shared --allow-multiple-definition --version-script pr21703.ver tmpdir/pr21703-3.o tmpdir/pr21703-4.o" "" "" \
142          {pr21703-3.s pr21703-4.s} {{readelf {--dyn-syms} pr21703-shared.sd}} "pr21703.so" }
143     } "d30v-*-*" "dlx-*-*" "pj-*-*" "tic6x-*-*" "hppa64-*-*"
144
145     # This target requires extra GAS options when building non-PIC code
146     # for linking with shared libraries.
147     set AFLAGS_NONPIC ""
148     if [istarget "mips*-*-*"] {
149         append AFLAGS_NONPIC " -call_nonpic"
150     }
151
152     # Run a test to check linking a shared library with a broken linker
153     # script that accidentally marks dynamic sections as notes.  The
154     # resulting executable is not expected to work, but the linker
155     # should not seg-fault whilst creating the binary.
156     # Note: setup_xfail before run_ld_link_tests xfails only the first test.
157     setup_xfail "tic6x-*-*"
158     run_ld_link_tests {
159         {"Build shared library for broken linker scrip test"
160             "-shared --hash-style=sysv" "" "" "note-3.s" {} "note-3.so" }
161         {"Link using broken linker script"
162             "--script note-3.t tmpdir/note-3.so" "" "" ""
163             { { ld "note-3.l" } }
164             "a.out" }
165     }
166
167     setup_xfail "tic6x-*-*"
168     run_ld_link_tests {
169         {"Build pr17068.so"
170             "-shared" "" ""
171             {pr17068d.s} {} "pr17068.so"}
172         {"Build pr17068a.a"
173             "" "" ""
174             {pr17068a.s pr17068c.s pr17068ez.s} {} "pr17068a.a"}
175         {"Build pr17068b.a"
176             "" "" ""
177             {pr17068b.s pr17068e.s} {} "pr17068b.a"}
178     }
179
180     # bfin does not currently support copy relocs.
181     run_ld_link_tests {
182         {"pr17068 link --as-needed lib in group"
183             "--as-needed --no-dynamic-linker" "--start-group tmpdir/pr17068a.a tmpdir/pr17068.so tmpdir/pr17068b.a --end-group" ""
184             {start.s pr17068.s} {} "pr17068"}
185     } "bfin-*-*"
186
187     # xfail on tic6x due to non-PIC/non-PID warnings
188     # Fails on MIPS because ABI trickery means that a NULL reloc is emitted.
189     # Fails on bfin because relocations are not created.
190     run_ld_link_tests {
191         {"-Bsymbolic-functions"
192             "-shared -Bsymbolic-functions" "" ""
193             {symbolic-func.s} {{readelf {-r --wide} symbolic-func.r}}
194             "symbolic-func.so"}
195     } "tic6x-*-*" "mips*-*-*" "bfin-*-*"
196
197     # xfail on tic6x due to non-PIC/non-PID warnings
198     run_ld_link_tests {
199         {"Build pr20995.so"
200             "-shared" "" ""
201             {pr20995b.s} {} "pr20995.so"}
202     } "tic6x-*-*"
203
204     # xfail on arm*-*-eabi*.  The list can be enlarged to those targets that
205     # don't support GNU_RELRO.  For more details, please see discussions at:
206     #   https://sourceware.org/ml/binutils/2017-01/msg00441.html
207     run_ld_link_tests {
208         {"Build pr20995-2.so"
209             "-shared -z relro" "" ""
210             {pr20995c.s} {{readelf {-l --wide} pr20995-2so.r}} "pr20995-2.so"}
211     } "tic6x-*-*" "arm*-*-eabi*" "hppa*64*-*-hpux*"
212
213     # These targets don't copy dynamic variables into .bss.
214     setup_xfail "alpha-*-*" "bfin-*-*" "ia64-*-*" "xtensa-*-*"
215     # or don't have .data.rel.ro
216     setup_xfail "hppa*64*-*-hpux*"
217     run_ld_link_tests [list \
218         [list \
219             "pr20995" \
220             "" "tmpdir/pr20995.so" "$AFLAGS_NONPIC" \
221             {pr20995a.s} {{readelf {-S --wide} pr20995.r}} "pr20995"]]
222
223     # xfail on arm*-*-eabi* is particularly because of no support of GNU_RELRO.
224     # Please see the link above for details.
225     setup_xfail "alpha-*-*" "bfin-*-*" "ia64-*-*" "xtensa-*-*" "arm*-*-eabi*"
226     setup_xfail "hppa*64*-*-hpux*"
227     run_ld_link_tests [list \
228         [list \
229             "pr20995-2" \
230             "" "tmpdir/pr20995-2.so" "$AFLAGS_NONPIC" \
231             {pr20995a.s} {{readelf {-S --wide} pr20995.r}} "pr20995-2"]]
232
233     setup_xfail "tic6x-*-*"
234     run_ld_link_tests {
235         {"Build pr22374 shared library"
236             "-shared" "" "" "pr22374b.s" {} "pr22374.so" }
237         {"pr22374 function pointer initialization"
238             "" "tmpdir/pr22374.so" "" "pr22374a.s"
239             { {readelf {--wide -r --dyn-syms} "pr22374-1.r"}
240               {readelf {--wide -r} "pr22374-2.r"} }
241             "pr22374" }
242     }
243 }
244
245 if [is_underscore_target] {
246     set ASFLAGS "$ASFLAGS --defsym UNDERSCORE=1"
247 }
248
249 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
250 foreach t $test_list {
251     # We need to strip the ".d", but can leave the dirname.
252     verbose [file rootname $t]
253     run_dump_test [file rootname $t]
254 }
255
256 # Targets using the generic linker backend don't support generating
257 # an import library.
258 set xfail_implib ""
259 if [is_generic_elf] {
260     set xfail_implib "*-*-*"
261 }
262
263 # Check that the --out-implib option work correctly.
264 run_ld_link_tests [list \
265     [list "Generate empty import library" \
266         "--out-implib=tmpdir/implib.lib" "" \
267         [concat "--defsym NO_GLOBAL=1" $hpux] \
268         {implib.s} \
269         {{ld empty-implib.out}} \
270         "implib" ] \
271     [list "Generate import library" \
272         "--out-implib=tmpdir/implib.lib" "" \
273         $hpux \
274         {implib.s} \
275         {{readelf {-s tmpdir/implib.lib} implib.rd}} \
276         "implib" ] \
277 ] $xfail_implib
278
279 if { [istarget *-*-linux*]
280      || [istarget *-*-nacl*]
281      || [istarget *-*-gnu*] } {
282     run_ld_link_tests {
283         {"Weak symbols in dynamic objects 1 (support)"
284             "-shared" "" "" {weak-dyn-1a.s}
285             {}
286             "libweakdyn1a.so"}
287         {"Weak symbols in dynamic objects 1 (main test)"
288             "-shared tmpdir/libweakdyn1a.so -Tweak-dyn-1.ld" "" "" {weak-dyn-1b.s}
289             {{readelf {--relocs --wide} weak-dyn-1.rd}}
290             "libweakdyn1b.so"}
291     }
292 }
293
294 #v850 gas complains about .tbss.var section attributes.
295 if { [check_gc_sections_available] && ![istarget "v850-*-*"] } {
296     run_ld_link_tests {
297         {"--gc-sections on tls variable"
298             "--gc-section" "" "" {tls_gc.s} {} "tls_gc"}
299     }
300 }
301
302 if { [istarget *-*-*linux*]
303      || [istarget *-*-nacl*]
304      || [istarget *-*-gnu*] } {
305     run_ld_link_tests {
306         {"stack exec" "-z execstack" "" "" {stack.s}
307             {{readelf {-Wl} stack-exec.rd}} "stack-exec.exe"}
308         {"stack size" "-z stack-size=0x123400" "" "" {stack.s}
309             {{readelf {-Wl} stack-size.rd}} "stack-size.exe"}
310     }
311 }
312
313 set LDFLAGS $old_ldflags
314 set ASFLAGS $old_asflags
315
316 # Check to see if the C compiler works
317 if { [which $CC] == 0 } {
318     return
319 }
320
321 if [check_gc_sections_available] {
322     run_cc_link_tests {
323         {"PR ld/13195" "-Wl,--gc-sections" ""
324          {pr13195.c} {} "pr13195"}
325     }
326 }
327
328 set array_tests {
329     {"preinit array" "" ""
330         {preinit.c} "preinit" "preinit.out"}
331     {"init array" "" ""
332         {init.c} "init" "init.out"}
333     {"fini array" "" ""
334         {fini.c} "fini" "fini.out"}
335     {"init array mixed" "" ""
336         {init-mixed.c} "init-mixed" "init-mixed.out" "-I."}
337 }
338 set array_tests_pie {
339     {"PIE preinit array" "-pie" ""
340         {preinit.c} "preinit" "preinit.out" "-fPIE"}
341     {"PIE init array" "-pie" ""
342         {init.c} "init" "init.out" "-fPIE"}
343     {"PIE fini array" "-pie" ""
344         {fini.c} "fini" "fini.out" "-fPIE"}
345     {"PIE init array mixed" "-pie" ""
346         {init-mixed.c} "init-mixed" "init-mixed.out" "-I. -fPIE"}
347     {"PIE PR ld/14525" "-pie" ""
348         {pr14525.c} "pr14525" "pr14525.out" "-fPIE"}
349 }
350 set array_tests_static {
351     {"static preinit array" "-static" ""
352         {preinit.c} "preinit" "preinit.out"}
353     {"static init array" "-static" ""
354         {init.c} "init" "init.out"}
355     {"static fini array" "-static" ""
356         {fini.c} "fini" "fini.out"}
357     {"static init array mixed" "-static" ""
358         {init-mixed.c} "init-mixed" "init-mixed.out" "-I."}
359 }
360
361 # NetBSD ELF systems do not currently support the .*_array sections.
362 set xfails "*-*-netbsdelf*"
363 run_ld_link_exec_tests $array_tests $xfails
364
365 if { [istarget *-*-linux*]
366      || [istarget *-*-nacl*]
367      || [istarget *-*-gnu*] } {
368     run_ld_link_exec_tests $array_tests_pie $xfails
369
370     run_ld_link_exec_tests [list \
371         [list \
372             "Run mbind2a" \
373             "$NOPIE_LDFLAGS -Wl,-z,common-page-size=0x4000" \
374             "" \
375             { mbind2a.s mbind2b.c } \
376             "mbind2a" \
377             "pass.out" \
378             "-O2 -I../bfd" \
379         ] \
380         [list \
381             "Run mbind2b" \
382             "-static -Wl,-z,common-page-size=0x4000" \
383             "" \
384             { mbind2a.s mbind2b.c } \
385             "mbind2b" \
386             "pass.out" \
387             "-O2 -I../bfd" \
388         ] \
389     ]
390 }
391
392 # <http://www.gnu.org/software/hurd/open_issues/binutils.html#static>
393 # Be cautious to not XFAIL for *-*-linux-gnu*, *-*-kfreebsd-gnu*, etc.
394 switch -regexp $target_triplet {
395     ^\[^-\]*-\[^-\]*-gnu.*$ {
396         set xfails "*-*-*"
397     }
398 }
399 run_ld_link_exec_tests $array_tests_static $xfails
400
401 catch "exec rm -f tmpdir/preinit tmpdir/init tmpdir/fini tmpdir/init-mixed" status