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