bfd_elf_define_start_stop: Fix check
[binutils-gdb.git] / ld / testsuite / ld-elf / elf.exp
1 # Expect script for various ELF tests.
2 # Copyright (C) 2002-2018 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 }
238 if { ![istarget "alpha-*-*"]
239 && ![istarget "frv-*-*"]
240 && ![istarget "hppa*-*-*"]
241 && ![istarget "i?86-*-*"]
242 && ![istarget "ia64-*-*"]
243 && ![istarget "microblaze-*-*"]
244 && ![istarget "powerpc*-*-*"]
245 && ![istarget "x86_64-*-*"]
246 && ![istarget "xtensa-*-*"] } {
247 # The next test checks that copy relocs are not used unnecessarily,
248 # but that is just an optimization so don't complain loudly.
249 setup_xfail "*-*-*"
250 }
251 run_ld_link_tests {
252 {"pr22374 function pointer initialization"
253 "" "tmpdir/pr22374.so" "" "pr22374a.s"
254 { {readelf {--wide -r --dyn-syms} "pr22374-1.r"}
255 {readelf {--wide -r} "pr22374-2.r"} }
256 "pr22374" }
257 }
258 }
259
260 if [is_underscore_target] {
261 set ASFLAGS "$ASFLAGS --defsym UNDERSCORE=1"
262 }
263
264 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
265 foreach t $test_list {
266 # We need to strip the ".d", but can leave the dirname.
267 verbose [file rootname $t]
268 run_dump_test [file rootname $t]
269 }
270
271 # Targets using the generic linker backend don't support generating
272 # an import library.
273 set xfail_implib ""
274 if [is_generic_elf] {
275 set xfail_implib "*-*-*"
276 }
277
278 # Check that the --out-implib option work correctly.
279 run_ld_link_tests [list \
280 [list "Generate empty import library" \
281 "--out-implib=tmpdir/implib.lib" "" \
282 [concat "--defsym NO_GLOBAL=1" $hpux] \
283 {implib.s} \
284 {{ld empty-implib.out}} \
285 "implib" ] \
286 [list "Generate import library" \
287 "--out-implib=tmpdir/implib.lib" "" \
288 $hpux \
289 {implib.s} \
290 {{readelf {-s tmpdir/implib.lib} implib.rd}} \
291 "implib" ] \
292 ] $xfail_implib
293
294 if { [istarget *-*-linux*]
295 || [istarget *-*-nacl*]
296 || [istarget *-*-gnu*] } {
297 run_ld_link_tests {
298 {"Weak symbols in dynamic objects 1 (support)"
299 "-shared" "" "" {weak-dyn-1a.s}
300 {}
301 "libweakdyn1a.so"}
302 {"Weak symbols in dynamic objects 1 (main test)"
303 "-shared tmpdir/libweakdyn1a.so -Tweak-dyn-1.ld" "" "" {weak-dyn-1b.s}
304 {{readelf {--relocs --wide} weak-dyn-1.rd}}
305 "libweakdyn1b.so"}
306 }
307 }
308
309 #v850 gas complains about .tbss.var section attributes.
310 if { [check_gc_sections_available] && ![istarget "v850-*-*"] } {
311 run_ld_link_tests {
312 {"--gc-sections on tls variable"
313 "--gc-section" "" "" {tls_gc.s} {} "tls_gc"}
314 }
315 }
316
317 if { [istarget *-*-*linux*]
318 || [istarget *-*-nacl*]
319 || [istarget *-*-gnu*] } {
320 run_ld_link_tests {
321 {"stack exec" "-z execstack" "" "" {stack.s}
322 {{readelf {-Wl} stack-exec.rd}} "stack-exec.exe"}
323 {"stack size" "-z stack-size=0x123400" "" "" {stack.s}
324 {{readelf {-Wl} stack-size.rd}} "stack-size.exe"}
325 }
326 }
327
328 set LDFLAGS $old_ldflags
329 set ASFLAGS $old_asflags
330
331 # Check to see if the C compiler works
332 if { [which $CC] == 0 } {
333 return
334 }
335
336 if [check_gc_sections_available] {
337 run_cc_link_tests {
338 {"PR ld/13195" "-Wl,--gc-sections" ""
339 {pr13195.c} {} "pr13195"}
340 }
341 }
342
343 set array_tests {
344 {"preinit array" "" ""
345 {preinit.c} "preinit" "preinit.out"}
346 {"init array" "" ""
347 {init.c} "init" "init.out"}
348 {"fini array" "" ""
349 {fini.c} "fini" "fini.out"}
350 {"init array mixed" "" ""
351 {init-mixed.c} "init-mixed" "init-mixed.out" "-I."}
352 }
353 set array_tests_pie {
354 {"PIE preinit array" "-pie" ""
355 {preinit.c} "preinit" "preinit.out" "-fPIE"}
356 {"PIE init array" "-pie" ""
357 {init.c} "init" "init.out" "-fPIE"}
358 {"PIE fini array" "-pie" ""
359 {fini.c} "fini" "fini.out" "-fPIE"}
360 {"PIE init array mixed" "-pie" ""
361 {init-mixed.c} "init-mixed" "init-mixed.out" "-I. -fPIE"}
362 {"PIE PR ld/14525" "-pie" ""
363 {pr14525.c} "pr14525" "pr14525.out" "-fPIE"}
364 }
365 set array_tests_static {
366 {"static preinit array" "-static" ""
367 {preinit.c} "preinit" "preinit.out"}
368 {"static init array" "-static" ""
369 {init.c} "init" "init.out"}
370 {"static fini array" "-static" ""
371 {fini.c} "fini" "fini.out"}
372 {"static init array mixed" "-static" ""
373 {init-mixed.c} "init-mixed" "init-mixed.out" "-I."}
374 }
375
376 # NetBSD ELF systems do not currently support the .*_array sections.
377 set xfails "*-*-netbsdelf*"
378 run_ld_link_exec_tests $array_tests $xfails
379
380 if { [istarget *-*-linux*]
381 || [istarget *-*-nacl*]
382 || [istarget *-*-gnu*] } {
383 run_ld_link_exec_tests $array_tests_pie $xfails
384
385 run_ld_link_exec_tests [list \
386 [list \
387 "Run mbind2a" \
388 "$NOPIE_LDFLAGS -Wl,-z,common-page-size=0x4000" \
389 "" \
390 { mbind2a.s mbind2b.c } \
391 "mbind2a" \
392 "pass.out" \
393 "-O2 -I../bfd" \
394 ] \
395 [list \
396 "Run mbind2b" \
397 "-static -Wl,-z,common-page-size=0x4000" \
398 "" \
399 { mbind2a.s mbind2b.c } \
400 "mbind2b" \
401 "pass.out" \
402 "-O2 -I../bfd" \
403 ] \
404 ]
405 }
406
407 # <http://www.gnu.org/software/hurd/open_issues/binutils.html#static>
408 # Be cautious to not XFAIL for *-*-linux-gnu*, *-*-kfreebsd-gnu*, etc.
409 switch -regexp $target_triplet {
410 ^\[^-\]*-\[^-\]*-gnu.*$ {
411 set xfails "*-*-*"
412 }
413 }
414 run_ld_link_exec_tests $array_tests_static $xfails
415
416 catch "exec rm -f tmpdir/preinit tmpdir/init tmpdir/fini tmpdir/init-mixed" status