de97939f81e74f7512fcdcd05a13cce13a8f5f92
[external/binutils.git] / ld / testsuite / ld-mips-elf / mips-elf-flags.exp
1 #   Copyright (C) 2003-2018 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 if {![istarget mips*-*-*] || ![is_elf_format]} {
21     return
22 }
23
24 global ldemul
25 if {[istarget mips*-*-irix6*]} {
26     set ldemul "-melf32bsmip"
27 } elseif {[istarget mips*el-*-linux*]} {
28     set ldemul "-melf32ltsmip"
29 } elseif {[istarget mips*-*-linux*]} {
30     set ldemul "-melf32btsmip"
31 } elseif {[istarget mips*el-*-*freebsd*]} {
32     set ldemul "-melf32ltsmip_fbsd"
33 } elseif {[istarget mips*-*-*freebsd*]} {
34     set ldemul "-melf32btsmip_fbsd"
35 } else {
36     set ldemul ""
37 }
38
39 # Check is ld supported 32bit emulations.
40 proc check_ld_support_32bit { } {
41     global ld
42     global echo
43
44     set ld_output [remote_exec host $ld "-V"]
45     if [string match "*elf32*" $ld_output] then {
46         return 1
47     } else {
48         return 0
49     }
50 }
51
52 # Check args is 32bit abis.
53 proc check_is_32bit_args {arg} {
54
55     if { [string match "*-32*" $arg]
56          || [string match "*-mabi=32*" $arg]
57          || [string match "*-mabi=o64*" $arg]
58          || [string match "*-mgp32*" $arg] } {
59         return 1
60     } else {
61         return 0
62     }
63 }
64
65 # Assemble jr.s using each of the argument lists in ARGLIST.  Return the
66 # list of object files on success and an empty list on failure.
67 proc assemble_for_flags {arglist} {
68     global as srcdir subdir
69
70     set objs {}
71     set index 1
72
73     foreach args $arglist {
74         set obj "tmpdir/mips-flags-${index}.o"
75         if {![ld_assemble $as "$args $srcdir/$subdir/jr.s" $obj]} {
76             return ""
77         }
78         lappend objs $obj
79         incr index
80     }
81     return $objs
82 }
83
84 # Assemble a file using each set of arguments in ARGLIST.  Check that
85 # the objects can be linked together and that the `readelf -h' output
86 # includes each flag named in FLAGS.  Additional one or more arguments
87 # request the addition of the `-A' option to `readelf' invocation.  In
88 # this case check MIPS ABI Flags reported for the ISA to match the
89 # first of these arguments, the ISA Extension to match the second, and
90 # the ASEs listed are exactly the same as those listed in the third,
91 # passed as a TCL list.
92 proc good_combination {arglist flags args} {
93     global ld ldemul READELF
94
95     set finalobj "tmpdir/mips-flags.o"
96     set testname "MIPS compatible objects: $arglist"
97     set objs [assemble_for_flags $arglist]
98
99     foreach argsl $arglist {
100         if { [check_is_32bit_args $argsl] && ![check_ld_support_32bit] } {
101             unsupported $testname
102             return 0
103         }
104     }
105
106     if {$objs == ""} {
107         unresolved $testname
108     } elseif {![ld_link "$ld $ldemul" $finalobj "-r $objs"]} {
109         fail $testname
110     } else {
111         set A [expr {[llength $args] > 0} ? {"A"} : {""}]
112         set cmd "$READELF -h$A $finalobj"
113         send_log "$cmd\n"
114         set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]]]
115         set output [lindex $cmdret 1]
116         set cmdret [lindex $cmdret 0]
117         if {$cmdret != 0 \
118             || ![regexp "Flags: *(\[^\n\r\]*)" $output full gotflags]} {
119             unresolved $testname
120         } else {
121             set failed 0
122
123             # GOTFLAGS is a list of flags separated by ", ".
124             # Convert it to a tcl list.
125             regsub -all ", " $gotflags "," gotflags
126             set gotflags [split $gotflags ","]
127
128             foreach flag $flags {
129                 if {[lsearch -exact $gotflags $flag] < 0} {
130                     set failed 1
131                 }
132             }
133
134             set isa [string trim [lindex $args 0]]
135             if {$isa != ""
136                 && (![regexp "ISA: *(\[^\n\r\]*)" $output full gotisa]
137                     || ![string match $isa $gotisa])} {
138                 set failed 1
139             }
140
141             set ext [string trim [lindex $args 1]]
142             if {$ext != ""
143                 && (![regexp "ISA Extension: *(\[^\n\r\]*)" \
144                       $output full gotext]
145                     || ![string match $ext $gotext])} {
146                 set failed 1
147             }
148
149             set ases [string trim [lindex $args 2]]
150             if {[llength $ases] > 0} {
151                 if {![regexp "ASEs:\[\n\r\]+((?:\t\[^\n\r\]*\[\n\r\]+)*)" \
152                       $output full gotases]} {
153                     set failed 1
154                 } else {
155                     # GOTASES is a list of strings separated by tab and
156                     # line separator characters.  Convert it to a TCL list.
157                     regsub -all "\[\n\r\t\]+" $gotases "\n" gotases
158                     set gotases [split [string trim $gotases] "\n"]
159
160                     foreach ase $ases {
161                         set aseidx [lsearch -exact $gotases $ase]
162                         if {$aseidx >= 0} {
163                             set gotases [lreplace $gotases $aseidx $aseidx]
164                         } else {
165                             set failed 1
166                         }
167                     }
168                     if {[llength $gotases] > 0} {
169                         set failed 1
170                     }
171                 }
172             }
173
174             if {$failed} {
175                 fail $testname
176             } else {
177                 pass $testname
178             }
179         }
180     }
181 }
182
183 # Like good_combination, but check that the objects can't be linked
184 # together successfully and that the output includes MESSAGE.
185 proc bad_combination {arglist message} {
186     global link_output ld ldemul
187
188     set finalobj "tmpdir/mips-flags.o"
189     set testname "MIPS incompatible objects: $arglist"
190     set objs [assemble_for_flags $arglist]
191
192     foreach argsl $arglist {
193         if { [check_is_32bit_args $argsl] && ![check_ld_support_32bit] } {
194             unsupported $testname
195             return 0
196         }
197     }
198
199     if {$objs == ""} {
200         unresolved $testname
201     } elseif {[ld_link "$ld $ldemul" $finalobj "-r $objs"]
202               || [string first $message $link_output] < 0} {
203         fail $testname
204     } else {
205         pass $testname
206     }
207 }
208
209 # Routines to check for various kinds of incompatibility.
210
211 proc abi_conflict {arglist firstabi secondabi} {
212     bad_combination $arglist \
213       "linking $secondabi module with previous $firstabi modules"
214 }
215
216 proc isa_conflict {arglist firstisa secondisa} {
217     bad_combination $arglist \
218       "linking mips:$secondisa module with previous mips:$firstisa modules"
219 }
220
221 proc regsize_conflict {arglist} {
222     bad_combination $arglist \
223       "linking 32-bit code with 64-bit code"
224 }
225
226 abi_conflict { "-mabi=eabi -mgp32" "-mips4 -mabi=32" } EABI32 O32
227 abi_conflict { "-mips4 -mabi=o64"  "-mips3 -mabi=eabi" } O64 EABI64
228
229 isa_conflict { "-march=vr5500 -32"     "-march=sb1 -32" } 5500 sb1
230 isa_conflict { "-march=vr5400 -32"     "-march=4120 -32" } 5400 4120
231 isa_conflict { "-march=r3900 -32"      "-march=r6000 -32" } 3900 6000
232 isa_conflict { "-march=r4010 -32"      "-march=r4650 -32" } 4010 4650
233 isa_conflict { "-mips3 -mgp32 -32"     "-mips32 -32" } 4000 isa32
234 isa_conflict { "-march=sb1 -mgp32 -32" "-mips32r2 -32" } sb1 isa32r2
235 isa_conflict { "-march=sb1 -32"        "-mips64r2 -32" } sb1 isa64r2
236
237 isa_conflict { "-march=vr4100 -32"     "-march=r10000 -32" } 4100 8000
238 isa_conflict { "-march=r5900 -32"      "-march=vr4111 -32" } 5900 4111
239 isa_conflict { "-march=loongson2e -32" "-march=loongson2f -32" } loongson_2e loongson_2f
240 isa_conflict { "-march=loongson3a -32" "-march=loongson2f -32" } loongson_3a loongson_2f
241
242 isa_conflict { "-march=interaptiv-mr2 -32" \
243                "-march=r4010 -32" } interaptiv-mr2 4010
244 isa_conflict { "-march=interaptiv-mr2 -mnan=2008 -mfp64 -32" \
245                "-mips32r6 -32" } interaptiv-mr2 isa32r6
246 isa_conflict { "-march=interaptiv-mr2 -32" \
247                "-mips3 -32" } interaptiv-mr2 4000
248 isa_conflict { "-march=interaptiv-mr2 -32" \
249                "-mips64r2 -32" } interaptiv-mr2 isa64r2
250 isa_conflict { "-march=interaptiv-mr2 -32" \
251                "-march=octeon -32" } interaptiv-mr2 octeon
252
253 regsize_conflict { "-mips4 -mgp64 -mabi=o64"         "-mips2 -32" }
254 regsize_conflict { "-mips4 -mabi=o64"                "-mips4 -mabi=32" }
255 regsize_conflict { "-mips4 -mabi=eabi -mgp32"        "-mips4 -mabi=eabi -mgp64" }
256 regsize_conflict { "-march=vr5000 -mgp64 -mabi=o64"  "-march=vr5000 -mgp32 -32" }
257 regsize_conflict { "-mips32 -32"                     "-mips64 -mabi=o64" }
258 regsize_conflict { "-mips32r2 -32"                   "-mips64 -mabi=o64" }
259 regsize_conflict { "-mips32r2 -32"                   "-mips64r2 -mabi=o64" }
260
261 good_combination { "-mips4 -mgp32 -32"  "-mips2 -32" } { mips4 o32 }
262 good_combination { "-mips4 -mabi=32"    "-mips2 -32" } { mips4 o32 }
263 good_combination { "-mips2 -32"         "-mips4 -mabi=32" } { mips4 o32 }
264 good_combination { "-mips2 -mabi=eabi"  "-mips4 -mabi=eabi -mgp32" } { mips4 eabi32 }
265 good_combination { "-mips2 -32"         "-mips32 -32" "-mips32r2 -32" } { mips32r2 }
266 good_combination { "-mips1 -32"         "-mips32r2 -32" "-mips32 -32" } { mips32r2 }
267
268 good_combination { "-march=vr4100 -32"  "-march=vr4120 -32" } { 4120 }
269 good_combination { "-march=vr5400 -32"  "-march=vr5500 -32" "-mips4 -32" } { 5500 }
270 good_combination { "-mips3 -32"         "-mips4 -32"        "-march=sb1 -32"  "-mips5 -32" } { sb1 }
271 good_combination { "-mips1 -32"         "-march=3900 -32" } { 3900 }
272 good_combination { "-mips3 -32"         "-mips64r2 -32" "   -mips64 -32" } { mips64r2 }
273
274 good_combination { "-march=vr4120 -mabi=32"  "-mips3 -mabi=32" } { 4120 o32 }
275 good_combination { "-march=sb1 -mgp32 -32"   "-march=4000 -mgp32 -32" } { sb1 o32 }
276 good_combination { "-mips32 -mabi=32"        "-march=sb1 -mabi=32" } { sb1 o32 }
277 good_combination { "-mips64r2 -mabi=32"      "-mips32 -mabi=32" } { mips64r2 o32 }
278 good_combination { "-mips5 -mabi=o64"        "-mips64r2 -mabi=o64" } { mips64r2 o64 }
279
280 good_combination { "-march=interaptiv-mr2 -32" "-mips1 -32" } \
281                  { mips32r2 interaptiv-mr2 } \
282                  MIPS32r3 "Imagination interAptiv MR2" \
283                  { "DSP ASE" "Enhanced VA Scheme" "MT ASE" }
284 good_combination { "-march=interaptiv-mr2 -32" "-mips32r2 -32" } \
285                  { mips32r2 interaptiv-mr2 } \
286                  MIPS32r3 "Imagination interAptiv MR2" \
287                  { "DSP ASE" "Enhanced VA Scheme" "MT ASE" }
288 good_combination { "-march=interaptiv-mr2 -32" "-march=interaptiv -32" } \
289                  { mips32r2 interaptiv-mr2 } \
290                  MIPS32r3 "Imagination interAptiv MR2" \
291                  { "DSP ASE" "Enhanced VA Scheme" "MT ASE" }
292 good_combination { "-march=interaptiv-mr2 -32" "-mips32r3 -32" } \
293                  { mips32r2 interaptiv-mr2 } \
294                  MIPS32r3 "Imagination interAptiv MR2" \
295                  { "DSP ASE" "Enhanced VA Scheme" "MT ASE" }
296 good_combination { "-march=interaptiv-mr2 -32" "-mips32r3 -mips16 -32" } \
297                  { mips32r2 interaptiv-mr2 } \
298                  MIPS32r3 "Imagination interAptiv MR2" \
299                  { "DSP ASE" "Enhanced VA Scheme" "MT ASE" "MIPS16 ASE" }
300 good_combination { "-march=interaptiv-mr2 -mips16 -32" "-mips32r3 -32" } \
301                  { mips32r2 interaptiv-mr2 } \
302                  MIPS32r3 "Imagination interAptiv MR2" \
303                  { "DSP ASE" "Enhanced VA Scheme" "MT ASE" \
304                    "MIPS16 ASE" "MIPS16e2 ASE" }
305 good_combination { "-march=interaptiv-mr2 -32" "-mips32r5 -32" } \
306                  { mips32r2 interaptiv-mr2 } \
307                  MIPS32r5 "Imagination interAptiv MR2" \
308                  { "DSP ASE" "Enhanced VA Scheme" "MT ASE" }
309 good_combination { "-march=interaptiv-mr2 -32" "-march=m5100 -32" } \
310                  { mips32r2 interaptiv-mr2 } \
311                  MIPS32r5 "Imagination interAptiv MR2" \
312                  { "DSP ASE" "Enhanced VA Scheme" "MT ASE" }