Commit | Line | Data |
---|---|---|
a2b64bed NC |
1 | # Support routines for LD testsuite. |
2 | # Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 | |
3 | # Free Software Foundation, Inc. | |
4 | # | |
5 | # This file is free software; you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
7 | # the Free Software Foundation; either version 2 of the License, or | |
8 | # (at your option) any later version. | |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program; if not, write to the Free Software | |
17 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | # | |
252b5132 RH |
19 | # |
20 | # default_ld_version | |
21 | # extract and print the version number of ld | |
22 | # | |
23 | proc default_ld_version { ld } { | |
24 | global host_triplet | |
25 | ||
26 | if { [which $ld] == 0 } then { | |
27 | perror "$ld does not exist" | |
28 | exit 1 | |
29 | } | |
30 | ||
31 | catch "exec $ld --version" tmp | |
32 | set tmp [prune_warnings $tmp] | |
33 | regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number | |
34 | if [info exists number] then { | |
35 | clone_output "$ld $number\n" | |
36 | } | |
37 | } | |
38 | ||
39 | # | |
40 | # default_ld_relocate | |
41 | # link an object using relocation | |
42 | # | |
43 | proc default_ld_relocate { ld target objects } { | |
44 | global HOSTING_EMU | |
45 | global host_triplet | |
46 | ||
47 | if { [which $ld] == 0 } then { | |
48 | perror "$ld does not exist" | |
49 | return 0 | |
50 | } | |
51 | ||
52 | verbose -log "$ld $HOSTING_EMU -o $target -r $objects" | |
53 | ||
54 | catch "exec $ld $HOSTING_EMU -o $target -r $objects" exec_output | |
55 | set exec_output [prune_warnings $exec_output] | |
56 | if [string match "" $exec_output] then { | |
57 | return 1 | |
58 | } else { | |
59 | verbose -log "$exec_output" | |
60 | return 0 | |
61 | } | |
62 | } | |
63 | ||
1688b748 MH |
64 | # Check to see if ld is being invoked with a non-endian output format |
65 | ||
66 | proc is_endian_output_format { object_flags } { | |
67 | ||
68 | if {[string match "*-oformat binary*" $object_flags] || \ | |
69 | [string match "*-oformat ieee*" $object_flags] || \ | |
70 | [string match "*-oformat ihex*" $object_flags] || \ | |
71 | [string match "*-oformat netbsd-core*" $object_flags] || \ | |
72 | [string match "*-oformat srec*" $object_flags] || \ | |
73 | [string match "*-oformat tekhex*" $object_flags] || \ | |
74 | [string match "*-oformat trad-core*" $object_flags] } then { | |
75 | return 0 | |
76 | } else { | |
77 | return 1 | |
78 | } | |
79 | } | |
80 | ||
38e31547 NC |
81 | # Look for big-endian or little-endian switches in the multlib |
82 | # options and translate these into a -EB or -EL switch. Note | |
83 | # we cannot rely upon proc process_multilib_options to do this | |
84 | # for us because for some targets the compiler does not support | |
85 | # -EB/-EL but it does support -mbig-endian/-mlittle-endian, and | |
86 | # the site.exp file will include the switch "-mbig-endian" | |
87 | # (rather than "big-endian") which is not detected by proc | |
88 | # process_multilib_options. | |
89 | ||
90 | proc big_or_little_endian {} { | |
91 | ||
92 | if [board_info [target_info name] exists multilib_flags] { | |
93 | set tmp_flags " [board_info [target_info name] multilib_flags]"; | |
94 | ||
95 | foreach x $tmp_flags { | |
96 | case $x in { | |
1688b748 | 97 | {*big*endian eb EB -eb -EB} { |
38e31547 NC |
98 | set flags " -EB" |
99 | return $flags | |
100 | } | |
1688b748 | 101 | {*little*endian el EL -el -EL} { |
38e31547 NC |
102 | set flags " -EL" |
103 | return $flags | |
104 | } | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
109 | set flags "" | |
110 | return $flags | |
111 | } | |
252b5132 RH |
112 | |
113 | # | |
114 | # default_ld_link | |
115 | # link a program using ld | |
116 | # | |
117 | proc default_ld_link { ld target objects } { | |
118 | global HOSTING_EMU | |
119 | global HOSTING_CRT0 | |
120 | global HOSTING_LIBS | |
d1bcade6 | 121 | global LIBS |
252b5132 | 122 | global host_triplet |
6fc49d28 | 123 | global link_output |
252b5132 RH |
124 | |
125 | set objs "$HOSTING_CRT0 $objects" | |
d1bcade6 | 126 | set libs "$LIBS $HOSTING_LIBS" |
252b5132 RH |
127 | |
128 | if { [which $ld] == 0 } then { | |
129 | perror "$ld does not exist" | |
130 | return 0 | |
131 | } | |
1688b748 MH |
132 | |
133 | if [is_endian_output_format $objects] then { | |
134 | set flags [big_or_little_endian] | |
135 | } else { | |
136 | set flags "" | |
137 | } | |
38e31547 | 138 | verbose -log "$ld $HOSTING_EMU $flags -o $target $objs $libs" |
252b5132 | 139 | |
6fc49d28 L |
140 | catch "exec $ld $HOSTING_EMU $flags -o $target $objs $libs" link_output |
141 | set exec_output [prune_warnings $link_output] | |
142 | if [string match "" $link_output] then { | |
252b5132 RH |
143 | return 1 |
144 | } else { | |
6fc49d28 | 145 | verbose -log "$link_output" |
252b5132 RH |
146 | return 0 |
147 | } | |
148 | } | |
149 | ||
150 | # | |
151 | # default_ld_simple_link | |
152 | # link a program using ld, without including any libraries | |
153 | # | |
154 | proc default_ld_simple_link { ld target objects } { | |
155 | global host_triplet | |
7cda33a1 L |
156 | global link_output |
157 | ||
252b5132 RH |
158 | if { [which $ld] == 0 } then { |
159 | perror "$ld does not exist" | |
160 | return 0 | |
161 | } | |
162 | ||
1688b748 MH |
163 | if [is_endian_output_format $objects] then { |
164 | set flags [big_or_little_endian] | |
165 | } else { | |
166 | set flags "" | |
167 | } | |
38e31547 NC |
168 | |
169 | verbose -log "$ld $flags -o $target $objects" | |
252b5132 | 170 | |
7cda33a1 L |
171 | catch "exec $ld $flags -o $target $objects" link_output |
172 | set exec_output [prune_warnings $link_output] | |
252b5132 RH |
173 | |
174 | # We don't care if we get a warning about a non-existent start | |
175 | # symbol, since the default linker script might use ENTRY. | |
176 | regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output | |
177 | ||
178 | if [string match "" $exec_output] then { | |
179 | return 1 | |
180 | } else { | |
181 | verbose -log "$exec_output" | |
182 | return 0 | |
183 | } | |
184 | } | |
185 | ||
186 | # | |
187 | # default_ld_compile | |
188 | # compile an object using cc | |
189 | # | |
190 | proc default_ld_compile { cc source object } { | |
191 | global CFLAGS | |
192 | global srcdir | |
193 | global subdir | |
194 | global host_triplet | |
195 | global gcc_gas_flag | |
196 | ||
197 | set cc_prog $cc | |
198 | if {[llength $cc_prog] > 1} then { | |
199 | set cc_prog [lindex $cc_prog 0] | |
200 | } | |
201 | if {[which $cc_prog] == 0} then { | |
202 | perror "$cc_prog does not exist" | |
203 | return 0 | |
204 | } | |
205 | ||
206 | catch "exec rm -f $object" exec_output | |
207 | ||
208 | set flags "-I$srcdir/$subdir $CFLAGS" | |
209 | ||
210 | # If we are compiling with gcc, we want to add gcc_gas_flag to | |
211 | # flags. Rather than determine this in some complex way, we guess | |
212 | # based on the name of the compiler. | |
213 | if {[string match "*gcc*" $cc] || [string match "*++*" $cc]} then { | |
214 | set flags "$gcc_gas_flag $flags" | |
215 | } | |
216 | ||
38e31547 NC |
217 | if [board_info [target_info name] exists multilib_flags] { |
218 | append flags " [board_info [target_info name] multilib_flags]"; | |
219 | } | |
220 | ||
252b5132 RH |
221 | verbose -log "$cc $flags -c $source -o $object" |
222 | ||
223 | catch "exec $cc $flags -c $source -o $object" exec_output | |
224 | set exec_output [prune_warnings $exec_output] | |
225 | if [string match "" $exec_output] then { | |
226 | if {![file exists $object]} then { | |
227 | regexp ".*/(\[^/\]*)$" $source all dobj | |
228 | regsub "\\.c" $dobj ".o" realobj | |
229 | verbose "looking for $realobj" | |
230 | if {[file exists $realobj]} then { | |
231 | verbose -log "mv $realobj $object" | |
232 | catch "exec mv $realobj $object" exec_output | |
233 | set exec_output [prune_warnings $exec_output] | |
234 | if {![string match "" $exec_output]} then { | |
235 | verbose -log "$exec_output" | |
236 | perror "could not move $realobj to $object" | |
237 | return 0 | |
238 | } | |
239 | } else { | |
240 | perror "$object not found after compilation" | |
241 | return 0 | |
242 | } | |
243 | } | |
244 | return 1 | |
245 | } else { | |
246 | verbose -log "$exec_output" | |
247 | perror "$source: compilation failed" | |
248 | return 0 | |
249 | } | |
250 | } | |
251 | ||
252 | # | |
253 | # default_ld_assemble | |
254 | # assemble a file | |
255 | # | |
256 | proc default_ld_assemble { as source object } { | |
257 | global ASFLAGS | |
258 | global host_triplet | |
259 | ||
260 | if {[which $as] == 0} then { | |
261 | perror "$as does not exist" | |
262 | return 0 | |
263 | } | |
264 | ||
265 | if ![info exists ASFLAGS] { set ASFLAGS "" } | |
266 | ||
38e31547 NC |
267 | set flags [big_or_little_endian] |
268 | ||
269 | verbose -log "$as $flags $ASFLAGS -o $object $source" | |
252b5132 | 270 | |
38e31547 | 271 | catch "exec $as $flags $ASFLAGS -o $object $source" exec_output |
252b5132 RH |
272 | set exec_output [prune_warnings $exec_output] |
273 | if [string match "" $exec_output] then { | |
274 | return 1 | |
275 | } else { | |
276 | verbose -log "$exec_output" | |
277 | perror "$source: assembly failed" | |
278 | return 0 | |
279 | } | |
280 | } | |
281 | ||
282 | # | |
283 | # default_ld_nm | |
284 | # run nm on a file, putting the result in the array nm_output | |
285 | # | |
992c450d | 286 | proc default_ld_nm { nm nmflags object } { |
252b5132 RH |
287 | global NMFLAGS |
288 | global nm_output | |
289 | global host_triplet | |
290 | ||
291 | if {[which $nm] == 0} then { | |
292 | perror "$nm does not exist" | |
293 | return 0 | |
294 | } | |
295 | ||
77e0b0ef ILT |
296 | if {[info exists nm_output]} { |
297 | unset nm_output | |
298 | } | |
299 | ||
252b5132 RH |
300 | if ![info exists NMFLAGS] { set NMFLAGS "" } |
301 | ||
992c450d | 302 | verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out" |
252b5132 | 303 | |
992c450d | 304 | catch "exec $nm $NMFLAGS $nmflags $object >tmpdir/nm.out" exec_output |
252b5132 RH |
305 | set exec_output [prune_warnings $exec_output] |
306 | if [string match "" $exec_output] then { | |
307 | set file [open tmpdir/nm.out r] | |
308 | while { [gets $file line] != -1 } { | |
309 | verbose "$line" 2 | |
dbc37f89 | 310 | if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] { |
252b5132 RH |
311 | set name [string trimleft $name "_"] |
312 | verbose "Setting nm_output($name) to 0x$value" 2 | |
313 | set nm_output($name) 0x$value | |
314 | } | |
315 | } | |
316 | close $file | |
317 | return 1 | |
318 | } else { | |
319 | verbose -log "$exec_output" | |
320 | perror "$object: nm failed" | |
321 | return 0 | |
322 | } | |
323 | } | |
324 | ||
3e3f011f RS |
325 | # |
326 | # is_elf_format | |
327 | # true if the object format is known to be ELF | |
328 | # | |
329 | proc is_elf_format {} { | |
330 | if { ![istarget *-*-sysv4*] \ | |
331 | && ![istarget *-*-unixware*] \ | |
332 | && ![istarget *-*-elf*] \ | |
333 | && ![istarget *-*-eabi*] \ | |
334 | && ![istarget *-*-linux*] \ | |
335 | && ![istarget *-*-irix5*] \ | |
336 | && ![istarget *-*-irix6*] \ | |
337 | && ![istarget *-*-solaris2*] } { | |
338 | return 0 | |
339 | } | |
340 | ||
341 | if { [istarget *-*-linux*aout*] \ | |
342 | || [istarget *-*-linux*oldld*] } { | |
343 | return 0 | |
344 | } | |
345 | return 1 | |
346 | } | |
347 | ||
252b5132 RH |
348 | # |
349 | # simple_diff | |
350 | # compares two files line-by-line | |
351 | # returns differences if exist | |
352 | # returns null if file(s) cannot be opened | |
353 | # | |
354 | proc simple_diff { file_1 file_2 } { | |
355 | global target | |
356 | ||
357 | set eof -1 | |
358 | set differences 0 | |
359 | ||
360 | if [file exists $file_1] then { | |
361 | set file_a [open $file_1 r] | |
362 | } else { | |
363 | warning "$file_1 doesn't exist" | |
364 | return | |
365 | } | |
366 | ||
367 | if [file exists $file_2] then { | |
368 | set file_b [open $file_2 r] | |
369 | } else { | |
370 | fail "$file_2 doesn't exist" | |
371 | return | |
372 | } | |
373 | ||
374 | verbose "# Diff'ing: $file_1 $file_2\n" 2 | |
375 | ||
376 | while { [gets $file_a line] != $eof } { | |
377 | if [regexp "^#.*$" $line] then { | |
378 | continue | |
379 | } else { | |
380 | lappend list_a $line | |
381 | } | |
382 | } | |
383 | close $file_a | |
384 | ||
385 | while { [gets $file_b line] != $eof } { | |
386 | if [regexp "^#.*$" $line] then { | |
387 | continue | |
388 | } else { | |
389 | lappend list_b $line | |
390 | } | |
391 | } | |
392 | close $file_b | |
393 | ||
394 | for { set i 0 } { $i < [llength $list_a] } { incr i } { | |
395 | set line_a [lindex $list_a $i] | |
396 | set line_b [lindex $list_b $i] | |
397 | ||
398 | verbose "\t$file_1: $i: $line_a\n" 3 | |
399 | verbose "\t$file_2: $i: $line_b\n" 3 | |
400 | if [string compare $line_a $line_b] then { | |
401 | verbose -log "\t$file_1: $i: $line_a\n" | |
402 | verbose -log "\t$file_2: $i: $line_b\n" | |
403 | ||
404 | fail "Test: $target" | |
405 | return | |
406 | } | |
407 | } | |
408 | ||
409 | if { [llength $list_a] != [llength $list_b] } { | |
410 | fail "Test: $target" | |
411 | return | |
412 | } | |
413 | ||
414 | if $differences<1 then { | |
415 | pass "Test: $target" | |
416 | } | |
417 | } | |
418 | ||
261def70 HPN |
419 | # run_dump_test FILE |
420 | # Copied from gas testsuite, tweaked and further extended. | |
421 | # | |
422 | # Assemble a .s file, then run some utility on it and check the output. | |
423 | # | |
424 | # There should be an assembly language file named FILE.s in the test | |
425 | # suite directory, and a pattern file called FILE.d. `run_dump_test' | |
426 | # will assemble FILE.s, run some tool like `objdump', `objcopy', or | |
427 | # `nm' on the .o file to produce textual output, and then analyze that | |
428 | # with regexps. The FILE.d file specifies what program to run, and | |
429 | # what to expect in its output. | |
430 | # | |
431 | # The FILE.d file begins with zero or more option lines, which specify | |
432 | # flags to pass to the assembler, the program to run to dump the | |
433 | # assembler's output, and the options it wants. The option lines have | |
434 | # the syntax: | |
435 | # | |
436 | # # OPTION: VALUE | |
437 | # | |
438 | # OPTION is the name of some option, like "name" or "objdump", and | |
439 | # VALUE is OPTION's value. The valid options are described below. | |
440 | # Whitespace is ignored everywhere, except within VALUE. The option | |
441 | # list ends with the first line that doesn't match the above syntax | |
442 | # (hmm, not great for error detection). | |
443 | # | |
444 | # The interesting options are: | |
445 | # | |
446 | # name: TEST-NAME | |
447 | # The name of this test, passed to DejaGNU's `pass' and `fail' | |
448 | # commands. If omitted, this defaults to FILE, the root of the | |
449 | # .s and .d files' names. | |
450 | # | |
451 | # as: FLAGS | |
452 | # When assembling, pass FLAGS to the assembler. | |
453 | # If assembling several files, you can pass different assembler | |
454 | # options in the "source" directives. See below. | |
455 | # | |
456 | # ld: FLAGS | |
457 | # Link assembled files using FLAGS, in the order of the "source" | |
458 | # directives, when using multiple files. | |
459 | # | |
cfe5266f HPN |
460 | # objcopy_linked_file: FLAGS |
461 | # Run objcopy on the linked file with the specified flags. | |
462 | # This lets you transform the linked file using objcopy, before the | |
463 | # result is analyzed by an analyzer program specified below (which | |
464 | # may in turn *also* be objcopy). | |
465 | # | |
261def70 HPN |
466 | # PROG: PROGRAM-NAME |
467 | # The name of the program to run to analyze the .o file produced | |
468 | # by the assembler or the linker output. This can be omitted; | |
469 | # run_dump_test will guess which program to run by seeing which of | |
470 | # the flags options below is present. | |
471 | # | |
472 | # objdump: FLAGS | |
473 | # nm: FLAGS | |
474 | # objcopy: FLAGS | |
475 | # Use the specified program to analyze the assembler or linker | |
476 | # output file, and pass it FLAGS, in addition to the output name. | |
477 | # | |
478 | # source: SOURCE [FLAGS] | |
479 | # Assemble the file SOURCE.s using the flags in the "as" directive | |
480 | # and the (optional) FLAGS. If omitted, the source defaults to | |
481 | # FILE.s. | |
482 | # This is useful if several .d files want to share a .s file. | |
483 | # More than one "source" directive can be given, which is useful | |
484 | # when testing linking. | |
485 | # | |
486 | # xfail: TARGET | |
487 | # The test is expected to fail on TARGET. This may occur more than | |
488 | # once. | |
489 | # | |
490 | # target: TARGET | |
491 | # Only run the test for TARGET. This may occur more than once; the | |
492 | # target being tested must match at least one. | |
493 | # | |
494 | # notarget: TARGET | |
495 | # Do not run the test for TARGET. This may occur more than once; | |
496 | # the target being tested must not match any of them. | |
497 | # | |
498 | # error: REGEX | |
499 | # An error with message matching REGEX must be emitted for the test | |
500 | # to pass. The PROG, objdump, nm and objcopy options have no | |
501 | # meaning and need not supplied if this is present. | |
502 | # | |
503 | # Each option may occur at most once unless otherwise mentioned. | |
504 | # | |
505 | # After the option lines come regexp lines. `run_dump_test' calls | |
506 | # `regexp_diff' to compare the output of the dumping tool against the | |
507 | # regexps in FILE.d. `regexp_diff' is defined later in this file; see | |
508 | # further comments there. | |
509 | ||
510 | proc run_dump_test { name } { | |
511 | global subdir srcdir | |
512 | global OBJDUMP NM AS OBJCOPY READELF LD | |
513 | global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS LDFLAGS | |
514 | global host_triplet runtests | |
515 | ||
516 | if [string match "*/*" $name] { | |
517 | set file $name | |
518 | set name [file tail $name] | |
519 | } else { | |
520 | set file "$srcdir/$subdir/$name" | |
521 | } | |
522 | ||
523 | if ![runtest_file_p $runtests $name] then { | |
524 | return | |
525 | } | |
526 | ||
527 | set opt_array [slurp_options "${file}.d"] | |
528 | if { $opt_array == -1 } { | |
529 | perror "error reading options from $file.d" | |
530 | unresolved $subdir/$name | |
531 | return | |
532 | } | |
533 | set dumpfile tmpdir/dump.out | |
534 | set run_ld 0 | |
cfe5266f | 535 | set run_objcopy 0 |
261def70 HPN |
536 | set opts(as) {} |
537 | set opts(ld) {} | |
538 | set opts(xfail) {} | |
539 | set opts(target) {} | |
540 | set opts(notarget) {} | |
541 | set opts(objdump) {} | |
542 | set opts(nm) {} | |
543 | set opts(objcopy) {} | |
544 | set opts(readelf) {} | |
545 | set opts(name) {} | |
546 | set opts(PROG) {} | |
547 | set opts(source) {} | |
548 | set opts(error) {} | |
cfe5266f | 549 | set opts(objcopy_linked_file) {} |
b2da51b6 | 550 | set asflags(${file}.s) {} |
261def70 HPN |
551 | |
552 | foreach i $opt_array { | |
553 | set opt_name [lindex $i 0] | |
554 | set opt_val [lindex $i 1] | |
555 | if ![info exists opts($opt_name)] { | |
556 | perror "unknown option $opt_name in file $file.d" | |
557 | unresolved $subdir/$name | |
558 | return | |
559 | } | |
560 | ||
561 | switch -- $opt_name { | |
562 | xfail {} | |
563 | target {} | |
564 | notarget {} | |
565 | source { | |
566 | # Move any source-specific as-flags to a separate array to | |
567 | # simplify processing. | |
568 | if { [llength $opt_val] > 1 } { | |
569 | set asflags([lindex $opt_val 0]) [lrange $opt_val 1 end] | |
570 | set opt_val [lindex $opt_val 0] | |
571 | } else { | |
572 | set asflags($opt_val) {} | |
573 | } | |
574 | } | |
575 | default { | |
576 | if [string length $opts($opt_name)] { | |
577 | perror "option $opt_name multiply set in $file.d" | |
578 | unresolved $subdir/$name | |
579 | return | |
580 | } | |
581 | ||
582 | # A single "# ld:" with no options should do the right thing. | |
583 | if { $opt_name == "ld" } { | |
584 | set run_ld 1 | |
585 | } | |
cfe5266f HPN |
586 | # Likewise objcopy_linked_file. |
587 | if { $opt_name == "objcopy_linked_file" } { | |
588 | set run_objcopy 1 | |
589 | } | |
261def70 HPN |
590 | } |
591 | } | |
592 | set opts($opt_name) [concat $opts($opt_name) $opt_val] | |
593 | } | |
594 | ||
595 | # Decide early whether we should run the test for this target. | |
596 | if { [llength $opts(target)] > 0 } { | |
597 | set targmatch 0 | |
598 | foreach targ $opts(target) { | |
599 | if [istarget $targ] { | |
600 | set targmatch 1 | |
601 | break | |
602 | } | |
603 | } | |
604 | if { $targmatch == 0 } { | |
605 | return | |
606 | } | |
607 | } | |
608 | foreach targ $opts(notarget) { | |
609 | if [istarget $targ] { | |
610 | return | |
611 | } | |
612 | } | |
613 | ||
614 | if {$opts(PROG) != ""} { | |
615 | switch -- $opts(PROG) { | |
616 | objdump | |
617 | { set program objdump } | |
618 | nm | |
619 | { set program nm } | |
620 | objcopy | |
621 | { set program objcopy } | |
622 | readelf | |
623 | { set program readelf } | |
624 | default | |
625 | { perror "unrecognized program option $opts(PROG) in $file.d" | |
626 | unresolved $subdir/$name | |
627 | return } | |
628 | } | |
629 | } elseif { $opts(error) != "" } { | |
630 | # It's meaningless to require an output-testing method when we | |
631 | # expect an error. For simplicity, we fake an arbitrary method. | |
632 | set program "nm" | |
633 | } else { | |
634 | # Guess which program to run, by seeing which option was specified. | |
635 | set program "" | |
636 | foreach p {objdump objcopy nm readelf} { | |
637 | if {$opts($p) != ""} { | |
638 | if {$program != ""} { | |
639 | perror "ambiguous dump program in $file.d" | |
640 | unresolved $subdir/$name | |
641 | return | |
642 | } else { | |
643 | set program $p | |
644 | } | |
645 | } | |
646 | } | |
647 | if {$program == ""} { | |
648 | perror "dump program unspecified in $file.d" | |
649 | unresolved $subdir/$name | |
650 | return | |
651 | } | |
652 | } | |
653 | ||
654 | set progopts1 $opts($program) | |
655 | eval set progopts \$[string toupper $program]FLAGS | |
656 | eval set binary \$[string toupper $program] | |
657 | if { $opts(name) == "" } { | |
658 | set testname "$subdir/$name" | |
659 | } else { | |
660 | set testname $opts(name) | |
661 | } | |
662 | ||
663 | if { $opts(source) == "" } { | |
664 | set sourcefiles [list ${file}.s] | |
665 | } else { | |
666 | set sourcefiles {} | |
667 | foreach sf $opts(source) { | |
668 | lappend sourcefiles "$srcdir/$subdir/$sf" | |
669 | # Must have asflags indexed on source name. | |
670 | set asflags($srcdir/$subdir/$sf) $asflags($sf) | |
671 | } | |
672 | } | |
673 | ||
674 | # Time to setup xfailures. | |
675 | foreach targ $opts(xfail) { | |
676 | setup_xfail $targ | |
677 | } | |
678 | ||
679 | # Assemble each file. | |
680 | set objfiles {} | |
681 | for { set i 0 } { $i < [llength $sourcefiles] } { incr i } { | |
682 | set sourcefile [lindex $sourcefiles $i] | |
683 | ||
684 | set objfile "tmpdir/dump$i.o" | |
685 | lappend objfiles $objfile | |
686 | set cmd "$AS $ASFLAGS $opts(as) $asflags($sourcefile) -o $objfile $sourcefile" | |
687 | ||
688 | send_log "$cmd\n" | |
689 | set cmdret [catch "exec $cmd" comp_output] | |
690 | set comp_output [prune_warnings $comp_output] | |
691 | ||
692 | # We accept errors at assembly stage too, unless we're supposed to | |
693 | # link something. | |
694 | if { $cmdret != 0 || ![string match "" $comp_output] } then { | |
695 | send_log "$comp_output\n" | |
696 | verbose "$comp_output" 3 | |
697 | if { $opts(error) != "" && $run_ld == 0 } { | |
698 | if [regexp $opts(error) $comp_output] { | |
699 | pass $testname | |
700 | return | |
701 | } | |
702 | } | |
703 | fail $testname | |
704 | return | |
705 | } | |
706 | } | |
707 | ||
708 | # Perhaps link the file(s). | |
709 | if { $run_ld } { | |
710 | set objfile "tmpdir/dump" | |
3e3f011f RS |
711 | |
712 | # Add -L$srcdir/$subdir so that the linker command can use | |
713 | # linker scripts in the source directory. | |
714 | set cmd "$LD $LDFLAGS -L$srcdir/$subdir \ | |
715 | $opts(ld) -o $objfile $objfiles" | |
261def70 HPN |
716 | |
717 | send_log "$cmd\n" | |
718 | set cmdret [catch "exec $cmd" comp_output] | |
719 | set comp_output [prune_warnings $comp_output] | |
720 | ||
721 | if { $cmdret != 0 || ![string match "" $comp_output] } then { | |
722 | verbose -log "failed with: <$comp_output>, expected: <$opts(error)>" | |
723 | send_log "$comp_output\n" | |
724 | verbose "$comp_output" 3 | |
cfe5266f | 725 | if { $opts(error) != "" && $run_objcopy == 0 } { |
261def70 HPN |
726 | if [regexp $opts(error) $comp_output] { |
727 | pass $testname | |
728 | return | |
729 | } | |
730 | } | |
731 | fail $testname | |
732 | return | |
733 | } | |
cfe5266f HPN |
734 | |
735 | if { $run_objcopy } { | |
736 | set infile $objfile | |
737 | set objfile "tmpdir/dump1" | |
738 | ||
739 | # Note that we don't use OBJCOPYFLAGS here; any flags must be | |
740 | # explicitly specified. | |
741 | set cmd "$OBJCOPY $opts(objcopy_linked_file) $infile $objfile" | |
742 | ||
743 | send_log "$cmd\n" | |
744 | set cmdret [catch "exec $cmd" comp_output] | |
745 | set comp_output [prune_warnings $comp_output] | |
746 | ||
747 | if { $cmdret != 0 || ![string match "" $comp_output] } then { | |
748 | verbose -log "failed with: <$comp_output>, expected: <$opts(error)>" | |
749 | send_log "$comp_output\n" | |
750 | verbose "$comp_output" 3 | |
751 | if { $opts(error) != "" } { | |
752 | if [regexp $opts(error) $comp_output] { | |
753 | pass $testname | |
754 | return | |
755 | } | |
756 | } | |
757 | fail $testname | |
758 | return | |
759 | } | |
760 | } | |
261def70 HPN |
761 | } else { |
762 | set objfile "tmpdir/dump0.o" | |
763 | } | |
764 | ||
765 | # We must not have expected failure if we get here. | |
766 | if { $opts(error) != "" } { | |
767 | fail $testname | |
cfe5266f | 768 | return |
261def70 HPN |
769 | } |
770 | ||
771 | if { [which $binary] == 0 } { | |
772 | untested $testname | |
773 | return | |
774 | } | |
775 | ||
776 | if { $progopts1 == "" } { set $progopts1 "-r" } | |
777 | verbose "running $binary $progopts $progopts1" 3 | |
778 | ||
779 | # Objcopy, unlike the other two, won't send its output to stdout, | |
780 | # so we have to run it specially. | |
781 | if { $program == "objcopy" } { | |
782 | set cmd "$binary $progopts $progopts1 $objfile $dumpfile" | |
783 | send_log "$cmd\n" | |
784 | catch "exec $cmd" comp_output | |
785 | set comp_output [prune_warnings $comp_output] | |
786 | if ![string match "" $comp_output] then { | |
787 | send_log "$comp_output\n" | |
788 | fail $testname | |
789 | return | |
790 | } | |
791 | } else { | |
792 | set cmd "$binary $progopts $progopts1 $objfile > $dumpfile" | |
793 | send_log "$cmd\n" | |
794 | catch "exec $cmd" comp_output | |
795 | set comp_output [prune_warnings $comp_output] | |
796 | if ![string match "" $comp_output] then { | |
797 | send_log "$comp_output\n" | |
798 | fail $testname | |
799 | return | |
800 | } | |
801 | } | |
802 | ||
803 | verbose_eval {[file_contents $dumpfile]} 3 | |
804 | if { [regexp_diff $dumpfile "${file}.d"] } then { | |
805 | fail $testname | |
806 | verbose "output is [file_contents $dumpfile]" 2 | |
807 | return | |
808 | } | |
809 | ||
810 | pass $testname | |
811 | } | |
812 | ||
813 | proc slurp_options { file } { | |
814 | if [catch { set f [open $file r] } x] { | |
815 | #perror "couldn't open `$file': $x" | |
816 | perror "$x" | |
817 | return -1 | |
818 | } | |
819 | set opt_array {} | |
820 | # whitespace expression | |
821 | set ws {[ ]*} | |
822 | set nws {[^ ]*} | |
823 | # whitespace is ignored anywhere except within the options list; | |
cfe5266f HPN |
824 | # option names are alphabetic plus underscore only. |
825 | set pat "^#${ws}(\[a-zA-Z_\]*)$ws:${ws}(.*)$ws\$" | |
261def70 HPN |
826 | while { [gets $f line] != -1 } { |
827 | set line [string trim $line] | |
828 | # Whitespace here is space-tab. | |
829 | if [regexp $pat $line xxx opt_name opt_val] { | |
830 | # match! | |
831 | lappend opt_array [list $opt_name $opt_val] | |
832 | } else { | |
833 | break | |
834 | } | |
835 | } | |
836 | close $f | |
837 | return $opt_array | |
838 | } | |
839 | ||
840 | # regexp_diff, copied from gas, based on simple_diff above. | |
841 | # compares two files line-by-line | |
842 | # file1 contains strings, file2 contains regexps and #-comments | |
843 | # blank lines are ignored in either file | |
844 | # returns non-zero if differences exist | |
845 | # | |
846 | proc regexp_diff { file_1 file_2 } { | |
847 | ||
848 | set eof -1 | |
849 | set end_1 0 | |
850 | set end_2 0 | |
851 | set differences 0 | |
852 | set diff_pass 0 | |
853 | ||
854 | if [file exists $file_1] then { | |
855 | set file_a [open $file_1 r] | |
856 | } else { | |
857 | warning "$file_1 doesn't exist" | |
858 | return 1 | |
859 | } | |
860 | ||
861 | if [file exists $file_2] then { | |
862 | set file_b [open $file_2 r] | |
863 | } else { | |
864 | fail "$file_2 doesn't exist" | |
865 | close $file_a | |
866 | return 1 | |
867 | } | |
868 | ||
869 | verbose " Regexp-diff'ing: $file_1 $file_2" 2 | |
870 | ||
871 | while { 1 } { | |
872 | set line_a "" | |
873 | set line_b "" | |
874 | while { [string length $line_a] == 0 } { | |
875 | if { [gets $file_a line_a] == $eof } { | |
876 | set end_1 1 | |
877 | break | |
878 | } | |
879 | } | |
880 | while { [string length $line_b] == 0 || [string match "#*" $line_b] } { | |
881 | if [ string match "#pass" $line_b ] { | |
882 | set end_2 1 | |
883 | set diff_pass 1 | |
884 | break | |
885 | } elseif [ string match "#..." $line_b ] { | |
886 | if { [gets $file_b line_b] == $eof } { | |
887 | set end_2 1 | |
888 | break | |
889 | } | |
890 | verbose "looking for \"^$line_b$\"" 3 | |
891 | while { ![regexp "^$line_b$" "$line_a"] } { | |
892 | verbose "skipping \"$line_a\"" 3 | |
893 | if { [gets $file_a line_a] == $eof } { | |
894 | set end_1 1 | |
895 | break | |
896 | } | |
897 | } | |
898 | break | |
899 | } | |
900 | if { [gets $file_b line_b] == $eof } { | |
901 | set end_2 1 | |
902 | break | |
903 | } | |
904 | } | |
905 | ||
906 | if { $diff_pass } { | |
907 | break | |
908 | } elseif { $end_1 && $end_2 } { | |
909 | break | |
910 | } elseif { $end_1 } { | |
911 | send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n" | |
912 | verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3 | |
913 | set differences 1 | |
914 | break | |
915 | } elseif { $end_2 } { | |
916 | send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" | |
917 | verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3 | |
918 | set differences 1 | |
919 | break | |
920 | } else { | |
921 | verbose "regexp \"^$line_b$\"\nline \"$line_a\"" 3 | |
922 | if ![regexp "^$line_b$" "$line_a"] { | |
923 | send_log "regexp_diff match failure\n" | |
924 | send_log "regexp \"^$line_b$\"\nline \"$line_a\"\n" | |
925 | set differences 1 | |
926 | } | |
927 | } | |
928 | } | |
929 | ||
930 | if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } { | |
931 | send_log "$file_1 and $file_2 are different lengths\n" | |
932 | verbose "$file_1 and $file_2 are different lengths" 3 | |
933 | set differences 1 | |
934 | } | |
935 | ||
936 | close $file_a | |
937 | close $file_b | |
938 | ||
939 | return $differences | |
940 | } | |
941 | ||
942 | proc file_contents { filename } { | |
943 | set file [open $filename r] | |
944 | set contents [read $file] | |
945 | close $file | |
946 | return $contents | |
947 | } | |
948 | ||
949 | proc verbose_eval { expr { level 1 } } { | |
950 | global verbose | |
951 | if $verbose>$level then { eval verbose "$expr" $level } | |
952 | } | |
953 | ||
252b5132 RH |
954 | # This definition is taken from an unreleased version of DejaGnu. Once |
955 | # that version gets released, and has been out in the world for a few | |
956 | # months at least, it may be safe to delete this copy. | |
957 | if ![string length [info proc prune_warnings]] { | |
958 | # | |
959 | # prune_warnings -- delete various system verbosities from TEXT | |
960 | # | |
961 | # An example is: | |
962 | # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9 | |
963 | # | |
964 | # Sites with particular verbose os's may wish to override this in site.exp. | |
965 | # | |
966 | proc prune_warnings { text } { | |
967 | # This is from sun4's. Do it for all machines for now. | |
968 | # The "\\1" is to try to preserve a "\n" but only if necessary. | |
969 | regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text | |
970 | ||
971 | # It might be tempting to get carried away and delete blank lines, etc. | |
972 | # Just delete *exactly* what we're ask to, and that's it. | |
973 | return $text | |
974 | } | |
975 | } |