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