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