078e36cf2a9f9d6ff10b6d2634840b32d41a5362
[external/binutils.git] / ld / testsuite / ld-sh / arch / arch.exp
1 # Copyright (C) 2004-2015 Free Software Foundation, Inc.
2 #
3 # This file is part of the GNU Binutils.
4 #
5 # This program 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
18 # MA 02110-1301, USA.
19
20
21 # Please email any bugs, comments, and/or additions to this file to:
22 # binutils@sources.redhat.com
23
24 # This scripts tests of all available SH architectures with all other SH
25 # architectures. It ensures that those combinations which should not work
26 # do not work, and that those that should work produce the correct output
27 # architecture.
28 #
29 # It looks for files in the same directory as this file named sh*.s .
30 # Each file must contain one or more instructions which uniquely identifies
31 # that architecture. The architecture name is inferred from the file name.
32 # It is best to use the same files used by the assembler testsuite.
33 #
34 # It also creates another architecture named 'sh-unknown' by modifying
35 # another arch type (there is no way to assemble such an arch) in order
36 # to test what the linker would do with an older object file.
37 #
38 # The script generates the architecture permutations automatically,
39 # but it reads the expected results from the file arch_expected.txt (also
40 # found in the same directory as this script).
41 #
42 # The arch_expected.txt file should NOT be hand edited. Whenever the script
43 # is run (e.g. with 'make check') it creates a new (usually identical) file
44 # named arch_results.txt in the <objdir>/ld/testsuite directory. When the
45 # expected results change (or new architectures are added) this new file
46 # can be used to replace arch_expected.txt with no modification required.
47
48
49 # The procedure extracts the architecture name from the objdump output.
50 # If there is no architecture name (or objdump output changes significantly)
51 # then the behaviour is undefined, but it will most likely return junk.
52
53 proc get_sh_arch { ofile } {
54     global OBJDUMP
55
56     set cmd "$OBJDUMP -f $ofile"
57     verbose -log $cmd
58     catch "exec $cmd" objdump_output
59     verbose -log $objdump_output
60
61     set objdump_output [string replace $objdump_output 0 \
62             [expr [string first "architecture:" $objdump_output] + 13] ""]
63
64     return [string range $objdump_output 0 [expr [string first "," $objdump_output] - 1]]
65 }
66
67
68 # This procedure runs two tests:
69 #   Test 1: Check the linker can link the given files.
70 #   Test 2: Check that the resultant architecture is as expected.
71 # It also writes an entry to the arch_results.txt file.
72
73 proc test_arch { file1 file2 arch resultfile } {
74     global LD
75
76     set name1 [file tail $file1]
77     set rootname1 [file rootname $name1]
78
79     set name2 [file tail $file2]
80     set rootname2 [file rootname $name2]
81
82     set flags [big_or_little_endian]
83     
84     # This must use -r to prevent LD trying to relocate the (unrealistic) file
85     send_log "$LD $flags -r -o ${rootname1}_${rootname2}.o $file1 $file2\n"
86     catch "exec $LD $flags -r -o ${rootname1}_${rootname2}.o $file1 $file2" ld_output
87     send_log $ld_output
88
89     if {[string equal $ld_output ""] == 1} then {
90         pass "$rootname1 file should link with $rootname2 file"
91
92         set result [get_sh_arch "${rootname1}_${rootname2}.o"]
93         puts $resultfile [format "%-20s %-20s %s" $file1 $file2 $result]
94
95         if {$result == $arch} then {
96             pass "$rootname1 file with $rootname2 file should link to arch $arch"
97             file delete "${rootname1}_${rootname2}.o"
98         } else {
99             fail "$rootname1 file with $rootname2 file should link to arch $arch"
100         }
101     } else {
102         fail "$rootname1 file should link with $rootname2 file"
103
104         puts $resultfile [format "%-20s %-20s ERROR" $file1 $file2]
105         untested "$rootname2 file with $rootname2 file should link to arch $arch"
106     }
107
108 }
109
110
111
112 # This procedure tests that a pair of files that are not
113 # suposed to link does, in fact, not link.
114 # It also writes an entry to the arch_results.txt file.
115
116 proc test_arch_error { file1 file2 resultfile} {
117     global link_output LD
118
119     set name1 [file tail $file1]
120     set rootname1 [file rootname $name1]
121
122     set name2 [file tail $file2]
123     set rootname2 [file rootname $name2]
124
125     # This must use -r to prevent LD trying to relocate the (unrealistic) file
126     send_log "$LD -r -o ${rootname1}_${rootname2}.o $file1 $file2\n"
127     catch "exec $LD -r -o ${rootname1}_${rootname2}.o $file1 $file2" ld_output
128     send_log $ld_output
129
130     if {[string equal $ld_output ""] == 1} then {
131         fail "$rootname1 file should NOT link with $rootname2 file"
132         puts $resultfile [format "%-20s %-20s [get_sh_arch ${rootname1}_${rootname2}.o]" $file1 $file2]
133     } else {
134         pass "$rootname1 file should NOT link with $rootname2 file"
135         puts $resultfile [format "%-20s %-20s ERROR" $file1 $file2]
136     }
137 }   
138
139 # These tests are not suitable for sh-coff because
140 # coff does not store the architecture information.
141
142 if [istarget sh*-*-elf] then {
143     global subdir srcdir
144     global AS
145
146     # Find all the architectures and assemble all the files
147     # we will use for the linker tests.
148
149     set sfilelist [lsort -ascii [glob "$srcdir/$subdir/sh*.s"]]
150     set ofilelist {}
151     foreach sfile $sfilelist {
152         set ofile "[file rootname [file tail $sfile]].o"
153         lappend ofilelist $ofile
154
155         set endian "-big"
156         if [string equal [big_or_little_endian] " -EL"] then {
157             set endian "-little"
158         }
159
160         set cmd "$AS $endian -isa=any $sfile -o $ofile"
161         verbose -log $cmd
162         catch "exec $cmd" as_output
163         if ![file exists $ofile] then {
164             verbose -log $as_output
165             perror "$sfile: assembly failed"
166         }
167     }
168
169     # Create the default arch ofile
170     # This cannot be created with the assembler
171     # sh4al-dsp is number 6, sh-unknown is 0
172
173     lappend ofilelist "sh-unknown.o"
174
175     if [string equal [big_or_little_endian] " -EL"] then {
176         set cmd {xxd sh4al-dsp.o | sed {s/\(^0000020: .... .... \)06/\100/} | xxd -r - sh-unknown.o}
177     } else {
178         set cmd {xxd sh4al-dsp.o | sed {s/\(^0000020: .... .... .... ..\)06/\100/} | xxd -r - sh-unknown.o}
179     }
180     verbose -log $cmd
181     catch "exec $cmd" xxd_output
182     verbose -log $xxd_output
183     if [string equal [get_sh_arch "sh-unknown.o"] "sh4al-dsp"] then {
184         perror "sh-unknown.o not generated correctly"
185     }
186
187
188     # Initialise the results file
189
190     set outfile [open "arch_results.txt" w 0666]
191     puts $outfile "# Generated file. DO NOT EDIT"
192     puts $outfile "#"
193     puts $outfile "# This file is generated by ld/testsuite/ld-sh/arch/arch.exp ."
194     puts $outfile "# It contains the expected results of the tests."
195     puts $outfile "# If the tests are failing because the expected results"
196     puts $outfile "# have changed then run 'make check' and copy the new file"
197     puts $outfile "# from <objdir>/ld/arch_results.txt"
198     puts $outfile "# to   <srcdir>/ld/testsuite/ld-sh/arch/arch_expected.txt ."
199     puts $outfile "# Make sure the new expected results are ALL correct."
200     puts $outfile "#"
201     puts $outfile [format "# %-18s %-20s %s" "FILE1" "FILE2" "OUTPUT"]
202     puts $outfile [format "# %-18s %-20s %s" "-----" "-----" "------"]
203
204     # Open the expected results file and skip the header
205
206     set infile [open "$srcdir/$subdir/arch_expected.txt" r]
207     while {[gets $infile line] >= 0 && [string match {\#*} $line]} {verbose -log "reading '$line'"}
208
209     foreach file1 $ofilelist {
210         foreach file2 $ofilelist {
211             set name1 [file tail $file1]
212             set rootname1 [file rootname $name1]
213
214             set name2 [file tail $file2]
215             set rootname2 [file rootname $name2]
216
217             # Decode the expected result from the file
218
219             scan $line "%s %s %s" exfile1 exfile2 exarch
220             verbose -log "exfile1 = '$exfile1', exfile2 = '$exfile2', exarch = '$exarch'"
221             verbose -log "  name1 = '$name1',   name2 = '$name2'"
222
223             if {[string equal $exfile1 $name1] && [string equal $exfile2 $file2]} then {
224                 # The expected result file makes sense and
225                 # appears up-to-date (the file and options match)
226
227                 if {[string equal $exarch "ERROR"]} then {
228                     test_arch_error $file1 $file2 $outfile
229                 } else {
230                     test_arch $file1 $file2 $exarch $outfile
231                 }
232             } else {
233                 # The expected result file isn't right somehow
234                 # so just try any old test. This will cause
235                 # many failures, but will genrate the results file.
236
237                 test_arch $file1 $file2 $rootname1 $outfile
238             }
239
240             # Read the next line from the expected result file.
241             # This is at the end because the process of skipping
242             # the header reads the first real line
243
244             if [gets $infile line] then {
245                 verbose -log "reading '$line'"
246             }
247         }
248     }
249
250     close $infile
251     close $outfile
252
253     foreach file $ofilelist {
254         file delete $file
255     }
256 }