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