[ld/testsuite/]
[deliverable/binutils-gdb.git] / ld / testsuite / lib / ld-lib.exp
CommitLineData
a2b64bed 1# Support routines for LD testsuite.
59c108f7 2# Copyright 1994-2013 Free Software Foundation, Inc.
a2b64bed 3#
f96b4a7b
NC
4# This file is part of the GNU Binutils.
5#
a2b64bed
NC
6# This file is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
f96b4a7b 8# the Free Software Foundation; either version 3 of the License, or
a2b64bed 9# (at your option) any later version.
3e8cba19 10#
a2b64bed
NC
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.
3e8cba19 15#
a2b64bed
NC
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
f96b4a7b
NC
18# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19# MA 02110-1301, USA.
3b6fe0cc 20
f3097f33
RS
21proc load_common_lib { name } {
22 global srcdir
23 load_file $srcdir/../../binutils/testsuite/lib/$name
24}
25
26load_common_lib binutils-common.exp
27
fb35d3d8
DD
28# Returns 1 if the gcc for the target is at least version MAJOR.MINOR
29# Returns 0 otherwise.
30#
31proc at_least_gcc_version { major minor } {
5a68afcf 32
fb35d3d8
DD
33 if {![info exists CC]} {
34 set CC [find_gcc]
35 }
36 if { $CC == "" } {
37 return 0
38 }
39 set state [remote_exec host $CC --version]
40 set tmp "[lindex $state 1]\n"
41 # Look for (eg) 4.6.1 in the version output.
8b5b2228
MR
42 set ver_re "\[^\\.0-9\]+(\[1-9\]\[0-9\]*)\\.(\[0-9\]+)(?:\\.\[0-9\]+)?"
43 regexp $ver_re $tmp fred maj min
fb35d3d8 44 verbose "gcc version: $tmp"
8b5b2228
MR
45 if { ![info exists maj] || ![info exists min] } then {
46 perror "can't decipher gcc version number, fix the framework!"
47 return 0
48 }
fb35d3d8
DD
49 verbose "major gcc version is $maj, want at least $major"
50 if { $maj == $major } then {
51 verbose "minor gcc version is $min, want at least $minor"
8b5b2228 52 return [expr $min >= $minor]
fb35d3d8 53 } else {
8b5b2228 54 return [expr $maj > $major]
fb35d3d8
DD
55 }
56}
57
3b6fe0cc 58# Extract and print the version number of ld.
252b5132
RH
59#
60proc default_ld_version { ld } {
61 global host_triplet
62
7f6a71ff 63 if { ![is_remote host] && [which $ld] == 0 } then {
252b5132
RH
64 perror "$ld does not exist"
65 exit 1
66 }
3e8cba19 67
7f6a71ff
JM
68 remote_exec host "$ld --version" "" "/dev/null" "ld.version"
69 remote_upload host "ld.version"
70 set tmp [prune_warnings [file_contents "ld.version"]]
71 remote_file build delete "ld.version"
72 remote_file host delete "ld.version"
73
252b5132
RH
74 regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
75 if [info exists number] then {
76 clone_output "$ld $number\n"
77 }
78}
79
7f6a71ff
JM
80proc run_host_cmd { prog command } {
81 global link_output
3e8cba19 82
7f6a71ff
JM
83 if { ![is_remote host] && [which "$prog"] == 0 } then {
84 perror "$prog does not exist"
252b5132
RH
85 return 0
86 }
3e8cba19 87
7f6a71ff
JM
88 verbose -log "$prog $command"
89 set status [remote_exec host [concat sh -c [list "$prog $command 2>&1"]] "" "/dev/null" "ld.tmp"]
90 remote_upload host "ld.tmp"
91 set link_output [file_contents "ld.tmp"]
92 regsub "\n$" $link_output "" link_output
93 if { [lindex $status 0] != 0 && [string match "" $link_output] } then {
94 append link_output "child process exited abnormally"
95 }
96 remote_file build delete ld.tmp
97 remote_file host delete ld.tmp
fab4a87f 98
7f6a71ff
JM
99 if [string match "" $link_output] then {
100 return ""
101 }
3e8cba19 102
7f6a71ff
JM
103 verbose -log "$link_output"
104 return "$link_output"
105}
106
107proc run_host_cmd_yesno { prog command } {
108 global exec_output
109
110 set exec_output [prune_warnings [run_host_cmd "$prog" "$command"]]
252b5132 111 if [string match "" $exec_output] then {
7f6a71ff 112 return 1;
252b5132 113 }
7f6a71ff
JM
114 return 0;
115}
116
117# Link an object using relocation.
118#
119proc default_ld_relocate { ld target objects } {
120 global HOSTING_EMU
121
122 remote_file host delete $target
123 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU -o $target -r $objects"]
252b5132
RH
124}
125
1688b748 126# Check to see if ld is being invoked with a non-endian output format
3b6fe0cc 127#
1688b748
MH
128proc is_endian_output_format { object_flags } {
129
130 if {[string match "*-oformat binary*" $object_flags] || \
131 [string match "*-oformat ieee*" $object_flags] || \
132 [string match "*-oformat ihex*" $object_flags] || \
133 [string match "*-oformat netbsd-core*" $object_flags] || \
134 [string match "*-oformat srec*" $object_flags] || \
135 [string match "*-oformat tekhex*" $object_flags] || \
136 [string match "*-oformat trad-core*" $object_flags] } then {
137 return 0
138 } else {
139 return 1
140 }
141}
142
38e31547
NC
143# Look for big-endian or little-endian switches in the multlib
144# options and translate these into a -EB or -EL switch. Note
145# we cannot rely upon proc process_multilib_options to do this
146# for us because for some targets the compiler does not support
147# -EB/-EL but it does support -mbig-endian/-mlittle-endian, and
148# the site.exp file will include the switch "-mbig-endian"
149# (rather than "big-endian") which is not detected by proc
150# process_multilib_options.
3b6fe0cc 151#
38e31547 152proc big_or_little_endian {} {
3e8cba19 153
38e31547 154 if [board_info [target_info name] exists multilib_flags] {
b24f926d 155 set tmp_flags " [board_info [target_info name] multilib_flags]"
38e31547
NC
156
157 foreach x $tmp_flags {
158 case $x in {
906156c4 159 {*big*endian eb EB -eb -EB -mb -meb} {
38e31547
NC
160 set flags " -EB"
161 return $flags
162 }
906156c4 163 {*little*endian el EL -el -EL -ml -mel} {
38e31547
NC
164 set flags " -EL"
165 return $flags
166 }
167 }
168 }
169 }
170
171 set flags ""
172 return $flags
173}
252b5132 174
3b6fe0cc 175# Link a program using ld.
252b5132
RH
176#
177proc default_ld_link { ld target objects } {
178 global HOSTING_EMU
179 global HOSTING_CRT0
6738cadc 180 global HOSTING_SCRT0
252b5132 181 global HOSTING_LIBS
b27caf75 182 global HOSTING_SLIBS
d1bcade6 183 global LIBS
252b5132 184 global host_triplet
6fc49d28 185 global link_output
fab4a87f 186 global exec_output
3e8cba19 187
6738cadc
L
188 if { [ string match "* -pie *" $objects ] } {
189 set objs "$HOSTING_SCRT0 $objects"
b27caf75 190 set libs "$LIBS $HOSTING_SLIBS"
6738cadc
L
191 } else {
192 set objs "$HOSTING_CRT0 $objects"
b27caf75 193 set libs "$LIBS $HOSTING_LIBS"
6738cadc 194 }
3e8cba19 195
1688b748
MH
196 if [is_endian_output_format $objects] then {
197 set flags [big_or_little_endian]
198 } else {
199 set flags ""
200 }
fab4a87f 201
7f6a71ff 202 remote_file host delete $target
fab4a87f 203
7f6a71ff 204 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU $flags -o $target $objs $libs"]
252b5132
RH
205}
206
3b6fe0cc 207# Link a program using ld, without including any libraries.
252b5132
RH
208#
209proc default_ld_simple_link { ld target objects } {
210 global host_triplet
b765d4e3 211 global gcc_ld_flag
fab4a87f 212 global exec_output
7cda33a1 213
1688b748
MH
214 if [is_endian_output_format $objects] then {
215 set flags [big_or_little_endian]
216 } else {
217 set flags ""
218 }
3e8cba19 219
b765d4e3
L
220 # If we are compiling with gcc, we want to add gcc_ld_flag to
221 # flags. Rather than determine this in some complex way, we guess
222 # based on the name of the compiler.
b0fe1bf3
AM
223 set ldexe $ld
224 set ldparm [string first " " $ld]
cef3d14b 225 set ldflags ""
b0fe1bf3 226 if { $ldparm > 0 } then {
cef3d14b 227 set ldflags [string range $ld $ldparm end]
b0fe1bf3 228 set ldexe [string range $ld 0 $ldparm]
cef3d14b 229 set ld $ldexe
b0fe1bf3
AM
230 }
231 set ldexe [string replace $ldexe 0 [string last "/" $ldexe] ""]
0f84fde1 232 if {[string match "*gcc*" $ldexe] || [string match "*++*" $ldexe]} then {
cef3d14b 233 set ldflags "$gcc_ld_flag $ldflags"
b765d4e3
L
234 }
235
7f6a71ff 236 remote_file host delete $target
fab4a87f 237
cef3d14b 238 set exec_output [run_host_cmd "$ld" "$ldflags $flags -o $target $objects"]
7f6a71ff 239 set exec_output [prune_warnings $exec_output]
252b5132
RH
240
241 # We don't care if we get a warning about a non-existent start
242 # symbol, since the default linker script might use ENTRY.
243 regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
244
245 if [string match "" $exec_output] then {
246 return 1
247 } else {
252b5132
RH
248 return 0
249 }
250}
251
3b6fe0cc 252# Compile an object using cc.
252b5132
RH
253#
254proc default_ld_compile { cc source object } {
255 global CFLAGS
58ffc3bd 256 global CXXFLAGS
252b5132
RH
257 global srcdir
258 global subdir
259 global host_triplet
260 global gcc_gas_flag
261
262 set cc_prog $cc
263 if {[llength $cc_prog] > 1} then {
264 set cc_prog [lindex $cc_prog 0]
265 }
7f6a71ff 266 if {![is_remote host] && [which $cc_prog] == 0} then {
252b5132
RH
267 perror "$cc_prog does not exist"
268 return 0
269 }
270
7f6a71ff
JM
271 remote_file build delete "$object"
272 remote_file host delete "$object"
252b5132 273
58ffc3bd 274 set flags "-I$srcdir/$subdir"
252b5132
RH
275
276 # If we are compiling with gcc, we want to add gcc_gas_flag to
277 # flags. Rather than determine this in some complex way, we guess
278 # based on the name of the compiler.
b0fe1bf3
AM
279 set ccexe $cc
280 set ccparm [string first " " $cc]
dec20c9e 281 set ccflags ""
b0fe1bf3 282 if { $ccparm > 0 } then {
dec20c9e 283 set ccflags [string range $cc $ccparm end]
b0fe1bf3 284 set ccexe [string range $cc 0 $ccparm]
dec20c9e 285 set cc $ccexe
b0fe1bf3
AM
286 }
287 set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
0f84fde1 288 if {[string match "*gcc*" $ccexe] || [string match "*++*" $ccexe]} then {
252b5132
RH
289 set flags "$gcc_gas_flag $flags"
290 }
291
58ffc3bd
MF
292 if {[string match "*++*" $ccexe]} {
293 set flags "$flags $CXXFLAGS"
294 } else {
295 set flags "$flags $CFLAGS"
296 }
297
38e31547 298 if [board_info [target_info name] exists multilib_flags] {
b24f926d 299 append flags " [board_info [target_info name] multilib_flags]"
38e31547
NC
300 }
301
dec20c9e 302 verbose -log "$cc $flags $ccflags -c $source -o $object"
252b5132 303
7f6a71ff
JM
304 set status [remote_exec host [concat sh -c [list "$cc $flags $ccflags -c $source -o $object 2>&1"]] "" "/dev/null" "ld.tmp"]
305 remote_upload host "ld.tmp"
306 set exec_output [file_contents "ld.tmp"]
307 remote_file build delete "ld.tmp"
308 remote_file host delete "ld.tmp"
252b5132
RH
309 set exec_output [prune_warnings $exec_output]
310 if [string match "" $exec_output] then {
311 if {![file exists $object]} then {
312 regexp ".*/(\[^/\]*)$" $source all dobj
313 regsub "\\.c" $dobj ".o" realobj
314 verbose "looking for $realobj"
7f6a71ff 315 if {[remote_file host exists $realobj]} then {
252b5132 316 verbose -log "mv $realobj $object"
7f6a71ff 317 remote_upload "$realobj" "$object"
252b5132
RH
318 } else {
319 perror "$object not found after compilation"
320 return 0
321 }
322 }
323 return 1
324 } else {
325 verbose -log "$exec_output"
326 perror "$source: compilation failed"
327 return 0
328 }
329}
330
3b6fe0cc 331# Assemble a file.
252b5132 332#
de1491f0 333proc default_ld_assemble { as in_flags source object } {
252b5132
RH
334 global ASFLAGS
335 global host_triplet
690f47bf
RS
336 global srcdir
337 global subdir
3e8cba19 338
252b5132
RH
339 if ![info exists ASFLAGS] { set ASFLAGS "" }
340
690f47bf 341 set flags "[big_or_little_endian] -I$srcdir/$subdir"
de1491f0 342 set exec_output [run_host_cmd "$as" "$flags $in_flags $ASFLAGS -o $object $source"]
252b5132
RH
343 set exec_output [prune_warnings $exec_output]
344 if [string match "" $exec_output] then {
345 return 1
346 } else {
252b5132
RH
347 perror "$source: assembly failed"
348 return 0
349 }
350}
351
3b6fe0cc 352# Run nm on a file, putting the result in the array nm_output.
252b5132 353#
992c450d 354proc default_ld_nm { nm nmflags object } {
252b5132
RH
355 global NMFLAGS
356 global nm_output
357 global host_triplet
358
77e0b0ef
ILT
359 if {[info exists nm_output]} {
360 unset nm_output
361 }
362
252b5132
RH
363 if ![info exists NMFLAGS] { set NMFLAGS "" }
364
3e8cba19
AM
365 # Ensure consistent sorting of symbols
366 if {[info exists env(LC_ALL)]} {
367 set old_lc_all $env(LC_ALL)
368 }
369 set env(LC_ALL) "C"
7f6a71ff 370
992c450d 371 verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out"
252b5132 372
7f6a71ff 373 set status [remote_exec host [concat sh -c [list "$nm $NMFLAGS $nmflags $object 2>ld.stderr"]] "" "/dev/null" "tmpdir/nm.out"]
3e8cba19
AM
374 if {[info exists old_lc_all]} {
375 set env(LC_ALL) $old_lc_all
376 } else {
377 unset env(LC_ALL)
378 }
7f6a71ff
JM
379 remote_upload host "ld.stderr"
380 remote_upload host "tmpdir/nm.out" "tmpdir/nm.out"
381 set exec_output [prune_warnings [file_contents "ld.stderr"]]
382 remote_file host delete "ld.stderr"
383 remote_file build delete "ld.stderr"
252b5132
RH
384 if [string match "" $exec_output] then {
385 set file [open tmpdir/nm.out r]
386 while { [gets $file line] != -1 } {
387 verbose "$line" 2
dbc37f89 388 if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] {
252b5132
RH
389 set name [string trimleft $name "_"]
390 verbose "Setting nm_output($name) to 0x$value" 2
391 set nm_output($name) 0x$value
392 }
393 }
394 close $file
395 return 1
396 } else {
397 verbose -log "$exec_output"
398 perror "$object: nm failed"
399 return 0
400 }
401}
402
1b662205
AM
403# Define various symbols needed when not linking against all
404# target libs.
405proc ld_simple_link_defsyms {} {
406
407 set flags "--defsym __stack_chk_fail=0"
408
409 # ARM targets call __gccmain
8c5fc800 410 if {[istarget arm*-*-*]} {
1b662205
AM
411 append flags " --defsym __gccmain=0"
412 }
413
36fe835f
DK
414 # Windows targets need __main, prefixed with underscore.
415 if {[istarget *-*-cygwin* ] || [istarget *-*-mingw*]} {
416 append flags " --defsym ___main=0"
417 }
418
1b662205
AM
419 # PowerPC EABI code calls __eabi.
420 if {[istarget powerpc*-*-eabi*] || [istarget powerpc*-*-rtems*]} {
421 append flags " --defsym __eabi=0"
422 }
423
424 # mn10200 code calls __truncsipsi2_d0_d2.
425 if {[istarget mn10200*-*-*]} then {
426 append flags " --defsym __truncsipsi2_d0_d2=0"
427 }
428
429 # m6811/m6812 code has references to soft registers.
32d79e68 430 if {[istarget m6811-*-*] || [istarget m6812-*-*] || [istarget m68hc1*-*-*]} {
1b662205
AM
431 append flags " --defsym _.frame=0 --defsym _.d1=0 --defsym _.d2=0"
432 append flags " --defsym _.d3=0 --defsym _.d4=0"
433 append flags " --defsym _.tmp=0 --defsym _.xy=0 --defsym _.z=0"
434 }
435
436 # Some OpenBSD targets have ProPolice and reference __guard and
437 # __stack_smash_handler.
438 if [istarget *-*-openbsd*] {
439 append flags " --defsym __guard=0"
440 append flags " --defsym __stack_smash_handler=0"
441 }
442
443 return $flags
444}
445
ef2b5578 446# run_dump_test FILE (optional:) EXTRA_OPTIONS
261def70
HPN
447# Copied from gas testsuite, tweaked and further extended.
448#
449# Assemble a .s file, then run some utility on it and check the output.
3e8cba19 450#
261def70
HPN
451# There should be an assembly language file named FILE.s in the test
452# suite directory, and a pattern file called FILE.d. `run_dump_test'
453# will assemble FILE.s, run some tool like `objdump', `objcopy', or
454# `nm' on the .o file to produce textual output, and then analyze that
455# with regexps. The FILE.d file specifies what program to run, and
456# what to expect in its output.
457#
458# The FILE.d file begins with zero or more option lines, which specify
459# flags to pass to the assembler, the program to run to dump the
460# assembler's output, and the options it wants. The option lines have
461# the syntax:
3e8cba19 462#
261def70 463# # OPTION: VALUE
3e8cba19 464#
261def70
HPN
465# OPTION is the name of some option, like "name" or "objdump", and
466# VALUE is OPTION's value. The valid options are described below.
467# Whitespace is ignored everywhere, except within VALUE. The option
468# list ends with the first line that doesn't match the above syntax
469# (hmm, not great for error detection).
470#
ef2b5578
MR
471# The optional EXTRA_OPTIONS argument to `run_dump_test' is a list of
472# two-element lists. The first element of each is an option name, and
473# the second additional arguments to be added on to the end of the
474# option list as given in FILE.d. (If omitted, no additional options
475# are added.)
476#
261def70 477# The interesting options are:
3e8cba19 478#
261def70
HPN
479# name: TEST-NAME
480# The name of this test, passed to DejaGNU's `pass' and `fail'
481# commands. If omitted, this defaults to FILE, the root of the
482# .s and .d files' names.
3e8cba19 483#
261def70
HPN
484# as: FLAGS
485# When assembling, pass FLAGS to the assembler.
486# If assembling several files, you can pass different assembler
487# options in the "source" directives. See below.
488#
489# ld: FLAGS
490# Link assembled files using FLAGS, in the order of the "source"
491# directives, when using multiple files.
492#
d6e0b160
HPN
493# ld_after_inputfiles: FLAGS
494# Similar to "ld", but put after all input files.
495#
cfe5266f
HPN
496# objcopy_linked_file: FLAGS
497# Run objcopy on the linked file with the specified flags.
498# This lets you transform the linked file using objcopy, before the
499# result is analyzed by an analyzer program specified below (which
500# may in turn *also* be objcopy).
501#
261def70
HPN
502# PROG: PROGRAM-NAME
503# The name of the program to run to analyze the .o file produced
504# by the assembler or the linker output. This can be omitted;
505# run_dump_test will guess which program to run by seeing which of
506# the flags options below is present.
507#
508# objdump: FLAGS
509# nm: FLAGS
510# objcopy: FLAGS
511# Use the specified program to analyze the assembler or linker
512# output file, and pass it FLAGS, in addition to the output name.
3e8cba19
AM
513# Note that they are run with LC_ALL=C in the environment to give
514# consistent sorting of symbols.
261def70
HPN
515#
516# source: SOURCE [FLAGS]
517# Assemble the file SOURCE.s using the flags in the "as" directive
518# and the (optional) FLAGS. If omitted, the source defaults to
519# FILE.s.
520# This is useful if several .d files want to share a .s file.
521# More than one "source" directive can be given, which is useful
522# when testing linking.
523#
ef2b5578
MR
524# dump: DUMP
525# Match against DUMP.d. If omitted, this defaults to FILE.d. This
526# is useful if several .d files differ by options only. Options are
527# always read from FILE.d.
528#
261def70
HPN
529# xfail: TARGET
530# The test is expected to fail on TARGET. This may occur more than
531# once.
532#
533# target: TARGET
534# Only run the test for TARGET. This may occur more than once; the
33aa234e
JK
535# target being tested must match at least one. You may provide target
536# name "cfi" for any target supporting the CFI statements.
261def70
HPN
537#
538# notarget: TARGET
539# Do not run the test for TARGET. This may occur more than once;
540# the target being tested must not match any of them.
541#
542# error: REGEX
543# An error with message matching REGEX must be emitted for the test
544# to pass. The PROG, objdump, nm and objcopy options have no
164de317
HPN
545# meaning and need not supplied if this is present. Multiple "error"
546# directives append to the expected linker error message.
261def70 547#
bb00e284
HPN
548# warning: REGEX
549# Expect a linker warning matching REGEX. It is an error to issue
164de317
HPN
550# both "error" and "warning". Multiple "warning" directives
551# append to the expected linker warning message.
bb00e284 552#
261def70
HPN
553# Each option may occur at most once unless otherwise mentioned.
554#
555# After the option lines come regexp lines. `run_dump_test' calls
556# `regexp_diff' to compare the output of the dumping tool against the
eb22018c
RS
557# regexps in FILE.d. `regexp_diff' is defined in binutils-common.exp;
558# see further comments there.
3b6fe0cc 559#
ef2b5578 560proc run_dump_test { name {extra_options {}} } {
261def70
HPN
561 global subdir srcdir
562 global OBJDUMP NM AS OBJCOPY READELF LD
563 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS LDFLAGS
564 global host_triplet runtests
9a2ee7fc 565 global env verbose
261def70
HPN
566
567 if [string match "*/*" $name] {
568 set file $name
569 set name [file tail $name]
570 } else {
571 set file "$srcdir/$subdir/$name"
572 }
573
574 if ![runtest_file_p $runtests $name] then {
575 return
576 }
577
578 set opt_array [slurp_options "${file}.d"]
579 if { $opt_array == -1 } {
580 perror "error reading options from $file.d"
581 unresolved $subdir/$name
582 return
583 }
584 set dumpfile tmpdir/dump.out
585 set run_ld 0
cfe5266f 586 set run_objcopy 0
261def70
HPN
587 set opts(as) {}
588 set opts(ld) {}
d6e0b160 589 set opts(ld_after_inputfiles) {}
261def70
HPN
590 set opts(xfail) {}
591 set opts(target) {}
592 set opts(notarget) {}
593 set opts(objdump) {}
594 set opts(nm) {}
595 set opts(objcopy) {}
596 set opts(readelf) {}
597 set opts(name) {}
598 set opts(PROG) {}
599 set opts(source) {}
ef2b5578 600 set opts(dump) {}
261def70 601 set opts(error) {}
bb00e284 602 set opts(warning) {}
cfe5266f 603 set opts(objcopy_linked_file) {}
261def70
HPN
604
605 foreach i $opt_array {
606 set opt_name [lindex $i 0]
607 set opt_val [lindex $i 1]
608 if ![info exists opts($opt_name)] {
609 perror "unknown option $opt_name in file $file.d"
610 unresolved $subdir/$name
611 return
612 }
613
614 switch -- $opt_name {
615 xfail {}
616 target {}
617 notarget {}
164de317
HPN
618 warning {}
619 error {}
261def70 620 source {
df58fc94 621 # Move any source-specific as-flags to a separate list to
261def70
HPN
622 # simplify processing.
623 if { [llength $opt_val] > 1 } {
df58fc94 624 lappend asflags [lrange $opt_val 1 end]
261def70
HPN
625 set opt_val [lindex $opt_val 0]
626 } else {
df58fc94 627 lappend asflags {}
261def70
HPN
628 }
629 }
630 default {
631 if [string length $opts($opt_name)] {
632 perror "option $opt_name multiply set in $file.d"
633 unresolved $subdir/$name
634 return
635 }
636
637 # A single "# ld:" with no options should do the right thing.
638 if { $opt_name == "ld" } {
639 set run_ld 1
640 }
cfe5266f
HPN
641 # Likewise objcopy_linked_file.
642 if { $opt_name == "objcopy_linked_file" } {
643 set run_objcopy 1
644 }
261def70
HPN
645 }
646 }
7f6a71ff
JM
647 if { $opt_name == "as" || $opt_name == "ld" } {
648 set opt_val [subst $opt_val]
649 }
134fa82e
HPN
650
651 # Append differently whether it's a message (without space) or
652 # an option or list (with space).
653 switch -- $opt_name {
654 warning -
655 error {
656 append opts($opt_name) $opt_val
657 }
658 default {
659 set opts($opt_name) [concat $opts($opt_name) $opt_val]
660 }
661 }
261def70 662 }
ef2b5578
MR
663
664 foreach i $extra_options {
665 set opt_name [lindex $i 0]
666 set opt_val [lindex $i 1]
667 if ![info exists opts($opt_name)] {
668 perror "unknown option $opt_name given in extra_opts"
669 unresolved $subdir/$name
670 return
671 }
672 # Add extra option to end of existing option, adding space
673 # if necessary.
674 if { ![regexp "warning|error" $opt_name]
675 && [string length $opts($opt_name)] } {
676 append opts($opt_name) " "
677 }
678 append opts($opt_name) $opt_val
679 }
680
3935e1af
RS
681 foreach opt { as ld } {
682 regsub {\[big_or_little_endian\]} $opts($opt) \
683 [big_or_little_endian] opts($opt)
684 }
261def70
HPN
685
686 # Decide early whether we should run the test for this target.
687 if { [llength $opts(target)] > 0 } {
688 set targmatch 0
689 foreach targ $opts(target) {
690 if [istarget $targ] {
691 set targmatch 1
692 break
693 }
694 }
695 if { $targmatch == 0 } {
696 return
697 }
698 }
699 foreach targ $opts(notarget) {
700 if [istarget $targ] {
701 return
702 }
703 }
704
f364d1ca
AM
705 set program ""
706 # It's meaningless to require an output-testing method when we
707 # expect an error.
708 if { $opts(error) == "" } {
709 if {$opts(PROG) != ""} {
710 switch -- $opts(PROG) {
711 objdump { set program objdump }
712 nm { set program nm }
713 objcopy { set program objcopy }
714 readelf { set program readelf }
715 default
261def70
HPN
716 { perror "unrecognized program option $opts(PROG) in $file.d"
717 unresolved $subdir/$name
718 return }
f364d1ca
AM
719 }
720 } else {
261def70 721 # Guess which program to run, by seeing which option was specified.
f364d1ca
AM
722 foreach p {objdump objcopy nm readelf} {
723 if {$opts($p) != ""} {
724 if {$program != ""} {
725 perror "ambiguous dump program in $file.d"
726 unresolved $subdir/$name
727 return
728 } else {
729 set program $p
730 }
261def70
HPN
731 }
732 }
733 }
f364d1ca 734 if { $program == "" && $opts(warning) == "" } {
261def70
HPN
735 perror "dump program unspecified in $file.d"
736 unresolved $subdir/$name
737 return
738 }
739 }
740
261def70
HPN
741 if { $opts(name) == "" } {
742 set testname "$subdir/$name"
743 } else {
744 set testname $opts(name)
745 }
746
747 if { $opts(source) == "" } {
748 set sourcefiles [list ${file}.s]
df58fc94 749 set asflags [list ""]
261def70
HPN
750 } else {
751 set sourcefiles {}
752 foreach sf $opts(source) {
b7b0b729
HPN
753 if { [string match "/*" $sf] } {
754 lappend sourcefiles "$sf"
f364d1ca 755 } else {
b7b0b729
HPN
756 lappend sourcefiles "$srcdir/$subdir/$sf"
757 }
261def70
HPN
758 }
759 }
760
ef2b5578
MR
761 if { $opts(dump) == "" } {
762 set dfile ${file}.d
763 } else {
764 set dfile $srcdir/$subdir/$opts(dump)
765 }
766
65951855
RM
767 if { [string match "*--compress-debug-sections*" $opts(as)] \
768 && ![is_zlib_supported] } {
769 unsupported $testname
770 return
771 }
772
261def70
HPN
773 # Time to setup xfailures.
774 foreach targ $opts(xfail) {
775 setup_xfail $targ
776 }
777
778 # Assemble each file.
779 set objfiles {}
780 for { set i 0 } { $i < [llength $sourcefiles] } { incr i } {
781 set sourcefile [lindex $sourcefiles $i]
df58fc94 782 set sourceasflags [lindex $asflags $i]
261def70
HPN
783
784 set objfile "tmpdir/dump$i.o"
30dabe8a 785 catch "exec rm -f $objfile" exec_output
261def70 786 lappend objfiles $objfile
df58fc94 787 set cmd "$AS $ASFLAGS $opts(as) $sourceasflags -o $objfile $sourcefile"
261def70
HPN
788
789 send_log "$cmd\n"
7f6a71ff
JM
790 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
791 remote_upload host "ld.tmp"
792 set comp_output [prune_warnings [file_contents "ld.tmp"]]
793 remote_file host delete "ld.tmp"
794 remote_file build delete "ld.tmp"
261def70 795
7f6a71ff 796 if { [lindex $cmdret 0] != 0 || ![string match "" $comp_output] } then {
261def70
HPN
797 send_log "$comp_output\n"
798 verbose "$comp_output" 3
f364d1ca
AM
799
800 set exitstat "succeeded"
801 if { $cmdret != 0 } { set exitstat "failed" }
802 verbose -log "$exitstat with: <$comp_output>"
261def70
HPN
803 fail $testname
804 return
805 }
806 }
807
f364d1ca
AM
808 set expmsg $opts(error)
809 if { $opts(warning) != "" } {
810 if { $expmsg != "" } {
811 perror "$testname: mixing error and warning test-directives"
812 return
813 }
814 set expmsg $opts(warning)
815 }
816
261def70
HPN
817 # Perhaps link the file(s).
818 if { $run_ld } {
819 set objfile "tmpdir/dump"
30dabe8a 820 catch "exec rm -f $objfile" exec_output
3e3f011f
RS
821
822 # Add -L$srcdir/$subdir so that the linker command can use
823 # linker scripts in the source directory.
824 set cmd "$LD $LDFLAGS -L$srcdir/$subdir \
d6e0b160 825 $opts(ld) -o $objfile $objfiles $opts(ld_after_inputfiles)"
261def70
HPN
826
827 send_log "$cmd\n"
7f6a71ff
JM
828 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
829 remote_upload host "ld.tmp"
d3746675 830 set comp_output [file_contents "ld.tmp"]
7f6a71ff
JM
831 remote_file host delete "ld.tmp"
832 remote_file build delete "ld.tmp"
833 set cmdret [lindex $cmdret 0]
cfe5266f 834
f364d1ca 835 if { $cmdret == 0 && $run_objcopy } {
cfe5266f
HPN
836 set infile $objfile
837 set objfile "tmpdir/dump1"
7f6a71ff 838 remote_file host delete $objfile
cfe5266f
HPN
839
840 # Note that we don't use OBJCOPYFLAGS here; any flags must be
841 # explicitly specified.
842 set cmd "$OBJCOPY $opts(objcopy_linked_file) $infile $objfile"
843
844 send_log "$cmd\n"
7f6a71ff
JM
845 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
846 remote_upload host "ld.tmp"
d3746675 847 append comp_output [file_contents "ld.tmp"]
7f6a71ff
JM
848 remote_file host delete "ld.tmp"
849 remote_file build delete "ld.tmp"
850 set cmdret [lindex $cmdret 0]
f364d1ca
AM
851 }
852
7f6a71ff 853 regsub "\n$" $comp_output "" comp_output
f364d1ca
AM
854 if { $cmdret != 0 || $comp_output != "" || $expmsg != "" } then {
855 set exitstat "succeeded"
856 if { $cmdret != 0 } { set exitstat "failed" }
857 verbose -log "$exitstat with: <$comp_output>, expected: <$expmsg>"
858 send_log "$comp_output\n"
859 verbose "$comp_output" 3
860
164de317
HPN
861 if { ($expmsg == "") == ($comp_output == "") \
862 && [regexp $expmsg $comp_output] \
863 && (($cmdret == 0) == ($opts(error) == "")) } {
f364d1ca
AM
864 # We have the expected output from ld.
865 if { $opts(error) != "" || $program == "" } {
866 pass $testname
867 return
cfe5266f 868 }
f364d1ca
AM
869 } else {
870 verbose -log "$exitstat with: <$comp_output>, expected: <$expmsg>"
cfe5266f
HPN
871 fail $testname
872 return
873 }
874 }
261def70
HPN
875 } else {
876 set objfile "tmpdir/dump0.o"
877 }
878
879 # We must not have expected failure if we get here.
880 if { $opts(error) != "" } {
881 fail $testname
cfe5266f 882 return
261def70
HPN
883 }
884
f364d1ca
AM
885 set progopts1 $opts($program)
886 eval set progopts \$[string toupper $program]FLAGS
887 eval set binary \$[string toupper $program]
888
7f6a71ff 889 if { ![is_remote host] && [which $binary] == 0 } {
261def70
HPN
890 untested $testname
891 return
892 }
893
894 if { $progopts1 == "" } { set $progopts1 "-r" }
895 verbose "running $binary $progopts $progopts1" 3
896
897 # Objcopy, unlike the other two, won't send its output to stdout,
898 # so we have to run it specially.
3e8cba19 899 set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
261def70
HPN
900 if { $program == "objcopy" } {
901 set cmd "$binary $progopts $progopts1 $objfile $dumpfile"
3e8cba19
AM
902 }
903
904 # Ensure consistent sorting of symbols
905 if {[info exists env(LC_ALL)]} {
906 set old_lc_all $env(LC_ALL)
907 }
908 set env(LC_ALL) "C"
909 send_log "$cmd\n"
7f6a71ff 910 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>ld.tmp"]] "" "/dev/null"]
164de317 911 set cmdret [lindex $cmdret 0]
7f6a71ff
JM
912 remote_upload host "ld.tmp"
913 set comp_output [prune_warnings [file_contents "ld.tmp"]]
914 remote_file host delete "ld.tmp"
915 remote_file build delete "ld.tmp"
3e8cba19
AM
916 if {[info exists old_lc_all]} {
917 set env(LC_ALL) $old_lc_all
261def70 918 } else {
3e8cba19
AM
919 unset env(LC_ALL)
920 }
164de317
HPN
921 if { $cmdret != 0 || $comp_output != "" } {
922 send_log "exited abnormally with $cmdret, output:$comp_output\n"
3e8cba19
AM
923 fail $testname
924 return
261def70
HPN
925 }
926
9a2ee7fc 927 if { $verbose > 2 } then { verbose "output is [file_contents $dumpfile]" 3 }
ef2b5578 928 if { [regexp_diff $dumpfile "${dfile}"] } then {
261def70 929 fail $testname
9a2ee7fc 930 if { $verbose == 2 } then { verbose "output is [file_contents $dumpfile]" 2 }
261def70
HPN
931 return
932 }
933
934 pass $testname
935}
936
937proc slurp_options { file } {
5a68afcf
RM
938 # If options_regsub(foo) is set to {a b}, then the contents of a
939 # "#foo:" line will have regsub -all applied to replace a with b.
940 global options_regsub
941
261def70
HPN
942 if [catch { set f [open $file r] } x] {
943 #perror "couldn't open `$file': $x"
944 perror "$x"
945 return -1
946 }
947 set opt_array {}
948 # whitespace expression
949 set ws {[ ]*}
950 set nws {[^ ]*}
951 # whitespace is ignored anywhere except within the options list;
cfe5266f
HPN
952 # option names are alphabetic plus underscore only.
953 set pat "^#${ws}(\[a-zA-Z_\]*)$ws:${ws}(.*)$ws\$"
261def70
HPN
954 while { [gets $f line] != -1 } {
955 set line [string trim $line]
956 # Whitespace here is space-tab.
957 if [regexp $pat $line xxx opt_name opt_val] {
958 # match!
5a68afcf
RM
959 if [info exists options_regsub($opt_name)] {
960 set subst $options_regsub($opt_name)
961 regsub -all -- [lindex $subst 0] $opt_val [lindex $subst 1] \
962 opt_val
963 }
261def70
HPN
964 lappend opt_array [list $opt_name $opt_val]
965 } else {
966 break
967 }
968 }
969 close $f
970 return $opt_array
971}
972
261def70
HPN
973proc file_contents { filename } {
974 set file [open $filename r]
975 set contents [read $file]
976 close $file
977 return $contents
978}
bffbf940 979
5d3236ee
DK
980proc set_file_contents { filename contents } {
981 set file [open $filename w]
982 puts $file "$contents"
983 close $file
984}
985
d8880531
L
986# Create an archive using ar
987#
fa0a16b1 988proc ar_simple_create { ar aropts target objects } {
d8880531
L
989 remote_file host delete $target
990
d52a4dca 991 set exec_output [run_host_cmd "$ar" "$aropts -rc $target $objects"]
d8880531
L
992 set exec_output [prune_warnings $exec_output]
993
994 if [string match "" $exec_output] then {
995 send_log "$exec_output\n"
996 return 1
997 } else {
998 return 0
999 }
1000}
1001
9147e853
JJ
1002# List contains test-items with 3 items followed by 2 lists, one item and
1003# one optional item:
894891db 1004# 0:name
897aea50
MR
1005# 1:ld/ar leading options, placed before object files
1006# 2:ld/ar trailing options, placed after object files
1007# 3:assembler options
1008# 4:filenames of assembler files
1009# 5:list of actions, options and expected outputs.
1010# 6:name of output file
1011# 7:compiler flags (optional)
3b6fe0cc 1012#
894891db
NC
1013# Actions: { command command-line-options file-containg-expected-output-regexps }
1014# Commands:
1015# objdump: Apply objdump options on result.
1016# nm: Apply nm options on result.
1017# readelf: Apply readelf options on result.
5a68afcf 1018# ld: Don't apply anything on result. Compare output during linking with
894891db
NC
1019# the file containing regexps (which is the second arg, not the third).
1020# Note that this *must* be the first action if it is to be used at all;
1021# in all other cases, any output from the linker during linking is
1022# treated as a sign of an error and FAILs the test.
3b6fe0cc 1023#
bffbf940
JJ
1024proc run_ld_link_tests { ldtests } {
1025 global ld
1026 global as
1027 global nm
d8880531 1028 global ar
bffbf940
JJ
1029 global objdump
1030 global READELF
1031 global srcdir
1032 global subdir
1033 global env
9147e853
JJ
1034 global CC
1035 global CFLAGS
eca41774 1036 global runtests
5d3236ee 1037 global exec_output
bffbf940
JJ
1038
1039 foreach testitem $ldtests {
1040 set testname [lindex $testitem 0]
eca41774
DK
1041
1042 if ![runtest_file_p $runtests $testname] then {
1043 continue
1044 }
1045
bffbf940 1046 set ld_options [lindex $testitem 1]
897aea50
MR
1047 set ld_after [lindex $testitem 2]
1048 set as_options [lindex $testitem 3]
1049 set src_files [lindex $testitem 4]
1050 set actions [lindex $testitem 5]
1051 set binfile tmpdir/[lindex $testitem 6]
1052 set cflags [lindex $testitem 7]
bffbf940
JJ
1053 set objfiles {}
1054 set is_unresolved 0
1055 set failed 0
5d3236ee
DK
1056 set maybe_failed 0
1057 set ld_output ""
bffbf940
JJ
1058
1059# verbose -log "Testname is $testname"
1060# verbose -log "ld_options is $ld_options"
897aea50 1061# verbose -log "ld_after is $ld_after"
bffbf940 1062# verbose -log "as_options is $as_options"
9147e853 1063# verbose -log "src_files is $src_files"
bffbf940
JJ
1064# verbose -log "actions is $actions"
1065# verbose -log "binfile is $binfile"
1066
1067 # Assemble each file in the test.
9147e853 1068 foreach src_file $src_files {
74d44110
MR
1069 set fileroot "[file rootname [file tail $src_file]]"
1070 set objfile "tmpdir/$fileroot.o"
bffbf940
JJ
1071 lappend objfiles $objfile
1072
9147e853 1073 if { [file extension $src_file] == ".c" } {
74d44110 1074 set as_file "tmpdir/$fileroot.s"
9147e853
JJ
1075 if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
1076 set is_unresolved 1
1077 break
1078 }
1079 } else {
1080 set as_file "$srcdir/$subdir/$src_file"
1081 }
1082 if ![ld_assemble $as "$as_options $as_file" $objfile] {
bffbf940
JJ
1083 set is_unresolved 1
1084 break
1085 }
1086 }
1087
1088 # Catch assembler errors.
77c56f44 1089 if { $is_unresolved } {
bffbf940
JJ
1090 unresolved $testname
1091 continue
1092 }
1093
a7470592 1094 if { [regexp ".*\\.a$" $binfile] } {
897aea50 1095 if { ![ar_simple_create $ar $ld_options $binfile "$objfiles $ld_after"] } {
d8880531 1096 set failed 1
d8880531 1097 }
897aea50 1098 } elseif { ![ld_simple_link $ld $binfile "-L$srcdir/$subdir $ld_options $objfiles $ld_after"] } {
5d3236ee
DK
1099 set maybe_failed 1
1100 set ld_output "$exec_output"
d8880531
L
1101 }
1102
77c56f44 1103 if { !$failed } {
bffbf940
JJ
1104 foreach actionlist $actions {
1105 set action [lindex $actionlist 0]
1106 set progopts [lindex $actionlist 1]
1107
1108 # There are actions where we run regexp_diff on the
1109 # output, and there are other actions (presumably).
1110 # Handling of the former look the same.
1111 set dump_prog ""
1112 switch -- $action {
1113 objdump
1114 { set dump_prog $objdump }
1115 nm
1116 { set dump_prog $nm }
1117 readelf
1118 { set dump_prog $READELF }
5d3236ee
DK
1119 ld
1120 { set dump_prog "ld" }
bffbf940
JJ
1121 default
1122 {
1123 perror "Unrecognized action $action"
1124 set is_unresolved 1
1125 break
1126 }
1127 }
1128
5d3236ee 1129 if { $action == "ld" } {
894891db
NC
1130 set regexpfile $progopts
1131 verbose "regexpfile is $srcdir/$subdir/$regexpfile"
5d3236ee
DK
1132 set_file_contents "tmpdir/ld.messages" "$ld_output"
1133 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
894891db 1134 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$regexpfile"] } then {
5d3236ee
DK
1135 verbose "output is $ld_output" 2
1136 set failed 1
1137 break
1138 }
1139 set maybe_failed 0
77c56f44 1140 } elseif { !$maybe_failed && $dump_prog != "" } {
bffbf940
JJ
1141 set dumpfile [lindex $actionlist 2]
1142 set binary $dump_prog
1143
1144 # Ensure consistent sorting of symbols
1145 if {[info exists env(LC_ALL)]} {
1146 set old_lc_all $env(LC_ALL)
1147 }
1148 set env(LC_ALL) "C"
7f6a71ff
JM
1149 set cmd "$binary $progopts $binfile"
1150 set status [remote_exec host [concat sh -c [list "$cmd >dump.out 2>ld.stderr"]] "" "/dev/null"]
bffbf940 1151 send_log "$cmd\n"
7f6a71ff
JM
1152 remote_upload host "ld.stderr"
1153 set comp_output [prune_warnings [file_contents "ld.stderr"]]
1154 remote_file host delete "ld.stderr"
1155 remote_file build delete "ld.stderr"
5a68afcf 1156
bffbf940
JJ
1157 if {[info exists old_lc_all]} {
1158 set env(LC_ALL) $old_lc_all
1159 } else {
1160 unset env(LC_ALL)
1161 }
bffbf940
JJ
1162
1163 if ![string match "" $comp_output] then {
1164 send_log "$comp_output\n"
1165 set failed 1
1166 break
1167 }
1168
7f6a71ff
JM
1169 remote_upload host "dump.out"
1170
bffbf940
JJ
1171 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1172 verbose "output is [file_contents "dump.out"]" 2
1173 set failed 1
7f6a71ff
JM
1174 remote_file build delete "dump.out"
1175 remote_file host delete "dump.out"
bffbf940
JJ
1176 break
1177 }
7f6a71ff
JM
1178 remote_file build delete "dump.out"
1179 remote_file host delete "dump.out"
bffbf940
JJ
1180 }
1181 }
bffbf940
JJ
1182 }
1183
77c56f44 1184 if { $is_unresolved } {
bffbf940 1185 unresolved $testname
77c56f44
RS
1186 } elseif { $maybe_failed || $failed } {
1187 fail $testname
1188 } else {
1189 pass $testname
bffbf940
JJ
1190 }
1191 }
1192}
1193
252b5132
RH
1194# This definition is taken from an unreleased version of DejaGnu. Once
1195# that version gets released, and has been out in the world for a few
1196# months at least, it may be safe to delete this copy.
1197if ![string length [info proc prune_warnings]] {
1198 #
1199 # prune_warnings -- delete various system verbosities from TEXT
1200 #
1201 # An example is:
1202 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
1203 #
1204 # Sites with particular verbose os's may wish to override this in site.exp.
1205 #
1206 proc prune_warnings { text } {
1207 # This is from sun4's. Do it for all machines for now.
1208 # The "\\1" is to try to preserve a "\n" but only if necessary.
1209 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
1210
1211 # It might be tempting to get carried away and delete blank lines, etc.
1212 # Just delete *exactly* what we're ask to, and that's it.
1213 return $text
1214 }
1215}
24edc24d 1216
c8c140d9
BE
1217# targets_to_xfail is a list of target triplets to be xfailed.
1218# ldtests contains test-items with 3 items followed by 1 lists, 2 items
fab4a87f 1219# and 3 optional items:
c8c140d9
BE
1220# 0:name
1221# 1:ld options
1222# 2:assembler options
55255dae 1223# 3:filenames of source files
c8c140d9
BE
1224# 4:name of output file
1225# 5:expected output
1226# 6:compiler flags (optional)
55255dae 1227# 7:language (optional)
fab4a87f 1228# 8:linker warning (optional)
c8c140d9
BE
1229
1230proc run_ld_link_exec_tests { targets_to_xfail ldtests } {
24edc24d
L
1231 global ld
1232 global as
1233 global srcdir
1234 global subdir
1235 global env
1236 global CC
55255dae 1237 global CXX
24edc24d 1238 global CFLAGS
58ffc3bd 1239 global CXXFLAGS
22ec3bd1 1240 global errcnt
fab4a87f 1241 global exec_output
24edc24d
L
1242
1243 foreach testitem $ldtests {
c8c140d9
BE
1244 foreach target $targets_to_xfail {
1245 setup_xfail $target
1246 }
24edc24d
L
1247 set testname [lindex $testitem 0]
1248 set ld_options [lindex $testitem 1]
1249 set as_options [lindex $testitem 2]
1250 set src_files [lindex $testitem 3]
1251 set binfile tmpdir/[lindex $testitem 4]
1252 set expfile [lindex $testitem 5]
1253 set cflags [lindex $testitem 6]
55255dae 1254 set lang [lindex $testitem 7]
fab4a87f 1255 set warning [lindex $testitem 8]
24edc24d 1256 set objfiles {}
24edc24d
L
1257 set failed 0
1258
1259# verbose -log "Testname is $testname"
1260# verbose -log "ld_options is $ld_options"
1261# verbose -log "as_options is $as_options"
1262# verbose -log "src_files is $src_files"
24edc24d
L
1263# verbose -log "binfile is $binfile"
1264
1265 # Assemble each file in the test.
1266 foreach src_file $src_files {
74d44110
MR
1267 set fileroot "[file rootname [file tail $src_file]]"
1268 set objfile "tmpdir/$fileroot.o"
24edc24d
L
1269 lappend objfiles $objfile
1270
a10e6b21
L
1271 # We ignore warnings since some compilers may generate
1272 # incorrect section attributes and the assembler will warn
1273 # them.
58ffc3bd
MF
1274 if { [ string match "c++" $lang ] } {
1275 ld_compile "$CXX -c $CXXFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1276 } else {
1277 ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1278 }
cb5ab6c8 1279 }
a10e6b21 1280
cb5ab6c8
L
1281 # We have to use $CC to build PIE and shared library.
1282 if { [ string match "c" $lang ] } {
1283 set link_proc ld_simple_link
1284 set link_cmd $CC
1285 } elseif { [ string match "c++" $lang ] } {
1286 set link_proc ld_simple_link
1287 set link_cmd $CXX
1288 } elseif { [ string match "-shared" $ld_options ] \
1289 || [ string match "-pie" $ld_options ] } {
1290 set link_proc ld_simple_link
1291 set link_cmd $CC
1292 } else {
1293 set link_proc ld_link
1294 set link_cmd $ld
1295 }
24edc24d 1296
cb5ab6c8
L
1297 if ![$link_proc $link_cmd $binfile "-L$srcdir/$subdir $ld_options $objfiles"] {
1298 set failed 1
1299 } else {
1300 set failed 0
1301 }
1302
1303 # Check if exec_output is expected.
1304 if { $warning != "" } then {
1305 verbose -log "returned with: <$exec_output>, expected: <$warning>"
1306 if { [regexp $warning $exec_output] } then {
a10e6b21 1307 set failed 0
cb5ab6c8
L
1308 } else {
1309 set failed 1
fab4a87f 1310 }
cb5ab6c8 1311 }
fab4a87f 1312
cb5ab6c8
L
1313 if { $failed == 0 } {
1314 send_log "Running: $binfile > $binfile.out\n"
1315 verbose "Running: $binfile > $binfile.out"
1316 catch "exec $binfile > $binfile.out" exec_output
fab4a87f 1317
cb5ab6c8
L
1318 if ![string match "" $exec_output] then {
1319 send_log "$exec_output\n"
1320 verbose "$exec_output" 1
1321 set failed 1
1322 } else {
1323 send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
1324 verbose "diff $binfile.out $srcdir/$subdir/$expfile"
1325 catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
1326 set exec_output [prune_warnings $exec_output]
5a68afcf 1327
24edc24d
L
1328 if ![string match "" $exec_output] then {
1329 send_log "$exec_output\n"
1330 verbose "$exec_output" 1
1331 set failed 1
1332 }
1333 }
cb5ab6c8 1334 }
24edc24d 1335
cb5ab6c8
L
1336 if { $failed != 0 } {
1337 fail $testname
1338 } else {
1339 set errcnt 0
1340 pass $testname
24edc24d 1341 }
24edc24d
L
1342 }
1343}
d2dee3b2
L
1344
1345# List contains test-items with 3 items followed by 2 lists, one item and
1346# one optional item:
55255dae 1347# 0:name
fa0a16b1 1348# 1:ld or ar options
55255dae
L
1349# 2:compile options
1350# 3:filenames of source files
1351# 4:action and options.
1352# 5:name of output file
1353# 6:language (optional)
dd98f8d2 1354# 7:linker warnings (optional)
d2dee3b2
L
1355#
1356# Actions:
1357# objdump: Apply objdump options on result. Compare with regex (last arg).
1358# nm: Apply nm options on result. Compare with regex (last arg).
1359# readelf: Apply readelf options on result. Compare with regex (last arg).
1360#
1361proc run_cc_link_tests { ldtests } {
1362 global nm
1363 global objdump
1364 global READELF
1365 global srcdir
1366 global subdir
1367 global env
1368 global CC
55255dae 1369 global CXX
d2dee3b2 1370 global CFLAGS
58ffc3bd 1371 global CXXFLAGS
d8880531 1372 global ar
dd98f8d2 1373 global exec_output
d2dee3b2
L
1374
1375 foreach testitem $ldtests {
1376 set testname [lindex $testitem 0]
1377 set ldflags [lindex $testitem 1]
1378 set cflags [lindex $testitem 2]
1379 set src_files [lindex $testitem 3]
1380 set actions [lindex $testitem 4]
1381 set binfile tmpdir/[lindex $testitem 5]
55255dae 1382 set lang [lindex $testitem 6]
dd98f8d2 1383 set warnings [lindex $testitem 7]
d2dee3b2
L
1384 set objfiles {}
1385 set is_unresolved 0
1386 set failed 0
1387
1388 # Compile each file in the test.
1389 foreach src_file $src_files {
74d44110
MR
1390 set fileroot "[file rootname [file tail $src_file]]"
1391 set objfile "tmpdir/$fileroot.o"
d2dee3b2
L
1392 lappend objfiles $objfile
1393
1394 # We ignore warnings since some compilers may generate
1395 # incorrect section attributes and the assembler will warn
1396 # them.
58ffc3bd
MF
1397 if { [ string match "c++" $lang ] } {
1398 ld_compile "$CXX -c $CXXFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1399 } else {
1400 ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1401 }
d2dee3b2
L
1402 }
1403
1404 # Clear error and warning counts.
1405 reset_vars
1406
55255dae
L
1407 if { [ string match "c++" $lang ] } {
1408 set cc_cmd $CXX
1409 } else {
1410 set cc_cmd $CC
1411 }
1412
a7470592 1413 if { [regexp ".*\\.a$" $binfile] } {
fa0a16b1 1414 if { ![ar_simple_create $ar $ldflags $binfile "$objfiles"] } {
d8880531
L
1415 fail $testname
1416 set failed 1
1417 } else {
1418 set failed 0
1419 }
741e0128
L
1420 } else {
1421 if { ![ld_simple_link $cc_cmd $binfile "-L$srcdir/$subdir $ldflags $objfiles"] } {
1422 set failed 1
1423 } else {
1424 set failed 0
1425 }
1426
dd98f8d2
NC
1427 # Check if exec_output is expected.
1428 if { $warnings != "" } then {
1429 verbose -log "returned with: <$exec_output>, expected: <$warnings>"
1430 if { [regexp $warnings $exec_output] } then {
df71cb5c 1431 set failed 0
dd98f8d2
NC
1432 } else {
1433 set failed 1
1434 }
741e0128
L
1435 }
1436
1437 if { $failed == 1 } {
dd98f8d2 1438 fail $testname
dd98f8d2 1439 }
d8880531
L
1440 }
1441
1442 if { $failed == 0 } {
d2dee3b2
L
1443 foreach actionlist $actions {
1444 set action [lindex $actionlist 0]
1445 set progopts [lindex $actionlist 1]
1446
1447 # There are actions where we run regexp_diff on the
1448 # output, and there are other actions (presumably).
1449 # Handling of the former look the same.
1450 set dump_prog ""
1451 switch -- $action {
1452 objdump
1453 { set dump_prog $objdump }
1454 nm
1455 { set dump_prog $nm }
1456 readelf
1457 { set dump_prog $READELF }
1458 default
1459 {
1460 perror "Unrecognized action $action"
1461 set is_unresolved 1
1462 break
1463 }
1464 }
1465
1466 if { $dump_prog != "" } {
1467 set dumpfile [lindex $actionlist 2]
1468 set binary $dump_prog
1469
1470 # Ensure consistent sorting of symbols
1471 if {[info exists env(LC_ALL)]} {
1472 set old_lc_all $env(LC_ALL)
1473 }
1474 set env(LC_ALL) "C"
1475 set cmd "$binary $progopts $binfile > dump.out"
1476 send_log "$cmd\n"
1477 catch "exec $cmd" comp_output
1478 if {[info exists old_lc_all]} {
1479 set env(LC_ALL) $old_lc_all
1480 } else {
1481 unset env(LC_ALL)
1482 }
1483 set comp_output [prune_warnings $comp_output]
1484
1485 if ![string match "" $comp_output] then {
1486 send_log "$comp_output\n"
1487 set failed 1
1488 break
1489 }
1490
1491 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1492 verbose "output is [file_contents "dump.out"]" 2
1493 set failed 1
1494 break
1495 }
1496 }
1497 }
1498
1499 if { $failed != 0 } {
1500 fail $testname
1501 } else { if { $is_unresolved == 0 } {
1502 pass $testname
1503 } }
1504 }
1505
1506 # Catch action errors.
1507 if { $is_unresolved != 0 } {
1508 unresolved $testname
1509 continue
1510 }
1511 }
1512}
430a16a5
NC
1513
1514# Returns true if --gc-sections is supported on the target.
1515
1516proc check_gc_sections_available { } {
1517 global gc_sections_available_saved
1518 global ld
5a68afcf 1519
430a16a5
NC
1520 if {![info exists gc_sections_available_saved]} {
1521 # Some targets don't support gc-sections despite whatever's
1522 # advertised by ld's options.
59c108f7
NC
1523 if { [istarget arc-*-*]
1524 || [istarget d30v-*-*]
1525 || [istarget dlx-*-*]
1526 || [istarget i960-*-*]
1527 || [istarget or32-*-*]
1528 || [istarget pj*-*-*]
1529 || [istarget alpha-*-*]
1530 || [istarget hppa*64-*-*]
1531 || [istarget i370-*-*]
1532 || [istarget i860-*-*]
1533 || [istarget ia64-*-*]
1534 || [istarget mep-*-*]
1535 || [istarget mn10200-*-*]
1536 || [istarget *-*-cygwin]
1537 || [istarget *-*-mingw*] } {
430a16a5
NC
1538 set gc_sections_available_saved 0
1539 return 0
1540 }
1541
1542 # elf2flt uses -q (--emit-relocs), which is incompatible with
1543 # --gc-sections.
1544 if { [board_info target exists ldflags]
1545 && [regexp " -elf2flt\[ =\]" " [board_info target ldflags] "] } {
1546 set gc_sections_available_saved 0
1547 return 0
1548 }
1549
430a16a5 1550 # Check if the ld used by gcc supports --gc-sections.
1d5316ab
AM
1551 # FIXME: this test is useless since ld --help always says
1552 # --gc-sections is available
430a16a5
NC
1553 set ld_output [remote_exec host $ld "--help"]
1554 if { [ string first "--gc-sections" $ld_output ] >= 0 } {
1555 set gc_sections_available_saved 1
1556 } else {
1557 set gc_sections_available_saved 0
1558 }
1559 }
1560 return $gc_sections_available_saved
1561}
33aa234e 1562
1d5316ab
AM
1563# Returns true if -shared is supported on the target
1564# Only used and accurate for ELF targets at the moment
1565
1566proc check_shared_lib_support { } {
83a23418
YZ
1567 if {![istarget aarch64*-*-elf]
1568 && ![istarget arc-*-*]
1569 && ![istarget arm*-*-elf]
1d5316ab
AM
1570 && ![istarget avr-*-*]
1571 && ![istarget cr16-*-*]
5220199d 1572 && ![istarget cris*-*-elf]
1d5316ab
AM
1573 && ![istarget crx-*-*]
1574 && ![istarget d10v-*-*]
1575 && ![istarget d30v-*-*]
1576 && ![istarget dlx-*-*]
cfb8c092 1577 && ![istarget epiphany-*-*]
1d5316ab
AM
1578 && ![istarget fr30-*-*]
1579 && ![istarget frv-*-*]
1580 && ![istarget h8300-*-*]
1581 && ![istarget i860-*-*]
1582 && ![istarget i960-*-*]
1583 && ![istarget ip2k-*-*]
1584 && ![istarget iq2000-*-*]
1585 && ![istarget lm32-*-*]
1586 && ![istarget m32c-*-*]
1587 && ![istarget m32r-*-*]
32d79e68
AM
1588 && ![istarget m6811-*-*]
1589 && ![istarget m6812-*-*]
1d5316ab
AM
1590 && ![istarget m68hc1*-*-*]
1591 && ![istarget mcore*-*-*]
1592 && ![istarget mep-*-*]
1593 && ![istarget microblaze-*-*]
23c80bf4 1594 && ![istarget mips*-*-elf]
1d5316ab
AM
1595 && ![istarget mn10200-*-*]
1596 && ![istarget moxie-*-*]
1597 && ![istarget msp430-*-*]
1598 && ![istarget mt-*-*]
1599 && ![istarget openrisc-*-*]
1600 && ![istarget or32-*-*]
1601 && ![istarget pj-*-*]
4046d87a 1602 && ![istarget rl78-*-*]
1d5316ab
AM
1603 && ![istarget rx-*-*]
1604 && ![istarget spu-*-*]
1605 && ![istarget v850*-*-*]
1606 && ![istarget xstormy16-*-*]
1607 && ![istarget *-*-irix*]
1608 && ![istarget *-*-rtems] } {
1609 return 1
1610 }
1611 return 0
1612}
1613
5d3236ee
DK
1614# Returns true if the target ld supports the plugin API.
1615proc check_plugin_api_available { } {
1616 global plugin_api_available_saved
1617 global ld
1618 if {![info exists plugin_api_available_saved]} {
1619 # Check if the ld used by gcc supports --plugin.
1620 set ld_output [remote_exec host $ld "--help"]
1621 if { [ string first "-plugin" $ld_output ] >= 0 } {
1622 set plugin_api_available_saved 1
1623 } else {
1624 set plugin_api_available_saved 0
1625 }
1626 }
1627 return $plugin_api_available_saved
1628}
1629
3bd58fbe
L
1630# Returns true if the target compiler supports LTO
1631proc check_lto_available { } {
1632 global lto_available_saved
1633 global CC
1634 if {![info exists lto_available_saved]} {
1635 # Check if gcc supports -flto -fuse-linker-plugin
1636 if { [which $CC] == 0 } {
1637 set lto_available_saved 0
1638 return 0
1639 }
1640 set basename "lto"
1641 set src ${basename}[pid].c
1642 set output ${basename}[pid].so
1643 set f [open $src "w"]
1644 puts $f ""
1645 close $f
b35d44f1 1646 set status [remote_exec host $CC "-shared -B[pwd]/tmpdir/ld/ -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
3bd58fbe
L
1647 if { [lindex $status 0] == 0 } {
1648 set lto_available_saved 1
1649 } else {
1650 set lto_available_saved 0
1651 }
1652 file delete $src
1653 file delete $output
1654 }
1655 return $lto_available_saved
1656}
1657
33aa234e
JK
1658# Check if the assembler supports CFI statements.
1659
1660proc check_as_cfi { } {
1661 global check_as_cfi_result
1662 global as
1663 if [info exists check_as_cfi_result] {
1664 return $check_as_cfi_result
1665 }
1666 set as_file "tmpdir/check_as_cfi.s"
1667 set as_fh [open $as_file w 0666]
1668 puts $as_fh "# Generated file. DO NOT EDIT"
1669 puts $as_fh "\t.cfi_startproc"
1670 puts $as_fh "\t.cfi_endproc"
1671 close $as_fh
1672 remote_download host $as_file
1673 verbose -log "Checking CFI support:"
1674 rename "perror" "check_as_cfi_perror"
1675 proc perror { args } { }
1676 set success [ld_assemble $as $as_file "/dev/null"]
1677 rename "perror" ""
1678 rename "check_as_cfi_perror" "perror"
1679 #remote_file host delete $as_file
1680 set check_as_cfi_result $success
1681 return $success
1682}
1683
1684# Provide virtual target "cfi" for targets supporting CFI.
1685
1686rename "istarget" "istarget_ld"
1687proc istarget { target } {
1688 if {$target == "cfi"} {
1689 return [check_as_cfi]
1690 }
1691 return [istarget_ld $target]
1692}
This page took 0.737401 seconds and 4 git commands to generate.