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