]>
Commit | Line | Data |
---|---|---|
a2c58332 | 1 | # Copyright (C) 1994-2022 Free Software Foundation, Inc. |
252b5132 RH |
2 | |
3 | # This program is free software; you can redistribute it and/or modify | |
4 | # it under the terms of the GNU General Public License as published by | |
32866df7 | 5 | # the Free Software Foundation; either version 3 of the License, or |
252b5132 | 6 | # (at your option) any later version. |
21c40443 | 7 | # |
252b5132 RH |
8 | # This program is distributed in the hope that it will be useful, |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
21c40443 | 12 | # |
252b5132 RH |
13 | # You should have received a copy of the GNU General Public License |
14 | # along with this program; if not, write to the Free Software | |
b43b5d5f | 15 | # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
252b5132 RH |
16 | |
17 | # Please email any bugs, comments, and/or additions to this file to: | |
18 | # bug-dejagnu@prep.ai.mit.edu | |
19 | ||
20 | # Written by Ian Lance Taylor <ian@cygnus.com> | |
21 | ||
22 | if ![is_remote host] { | |
23 | if {[which $OBJCOPY] == 0} then { | |
24 | perror "$OBJCOPY does not exist" | |
25 | return | |
26 | } | |
27 | } | |
28 | ||
29 | send_user "Version [binutil_version $OBJCOPY]" | |
30 | ||
252b5132 | 31 | if ![is_remote host] { |
8d263650 BE |
32 | set tempfile tmpdir/bintest.o |
33 | set copyfile tmpdir/copy | |
252b5132 RH |
34 | } else { |
35 | set tempfile [remote_download host tmpdir/bintest.o] | |
36 | set copyfile copy | |
37 | } | |
38 | ||
39 | # Test that objcopy does not modify a file when copying it. | |
1fafefd5 | 40 | # "object" or "executable" values for type are supported. |
252b5132 | 41 | |
1fafefd5 | 42 | proc objcopy_test {testname srcfile type asflags ldflags} { |
64bb95af L |
43 | global OBJCOPY |
44 | global OBJCOPYFLAGS | |
45 | global srcdir | |
46 | global subdir | |
47 | global tempfile | |
48 | global copyfile | |
1fafefd5 JL |
49 | set t_tempfile $tempfile |
50 | set t_copyfile ${copyfile}.o | |
252b5132 | 51 | |
1fafefd5 JL |
52 | if { $type != "object" && $type != "executable" } { |
53 | error "objcopy_test accepts only \"object\" or \"executable\" values for type" | |
54 | } | |
55 | ||
56 | if {![binutils_assemble_flags $srcdir/$subdir/${srcfile} $t_tempfile "$asflags"]} then { | |
17e04eff | 57 | unsupported "objcopy $type ($testname)" |
1fafefd5 | 58 | remote_file host delete $t_tempfile |
64bb95af | 59 | return |
252b5132 | 60 | } |
252b5132 | 61 | |
6a81c59b | 62 | set xflags "" |
1fafefd5 JL |
63 | if { $type == "executable" } { |
64 | global LD | |
65 | # Check that LD exists | |
66 | if {[which $LD] == 0} then { | |
67 | untested "objcopy $type ($testname)" | |
68 | return | |
69 | } | |
6a81c59b | 70 | |
1fafefd5 JL |
71 | # Use tempfile and copyfile without the .o extension for executable files |
72 | set t_tempfile [string range $tempfile 0 end-2] | |
73 | set t_copyfile $copyfile | |
74 | set got [binutils_run $LD "$tempfile -o $t_tempfile $ldflags"] | |
75 | if { ![string equal "" $got] } then { | |
17e04eff | 76 | unsupported "objcopy $type ($testname)" |
1fafefd5 JL |
77 | return |
78 | } | |
00386881 | 79 | set xflags "--preserve-dates" |
985e0264 | 80 | sleep 1 |
1fafefd5 JL |
81 | } |
82 | ||
6a81c59b | 83 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $xflags $t_tempfile $t_copyfile"] |
64bb95af | 84 | |
21c40443 | 85 | if ![string equal "" $got] then { |
64ec7c3f | 86 | send_log "$got\n" |
1fafefd5 | 87 | fail "objcopy $type ($testname)" |
252b5132 | 88 | } else { |
1fafefd5 JL |
89 | send_log "cmp $t_tempfile $t_copyfile\n" |
90 | verbose "cmp $t_tempfile $t_copyfile" | |
64bb95af | 91 | if [is_remote host] { |
1fafefd5 JL |
92 | set src1 tmpdir/bintest |
93 | set src2 tmpdir/copy | |
94 | remote_upload host $t_tempfile $src1 | |
95 | remote_upload host $t_copyfile $src2 | |
64bb95af | 96 | } else { |
1fafefd5 JL |
97 | set src1 $t_tempfile |
98 | set src2 $t_copyfile | |
64bb95af L |
99 | } |
100 | set status [remote_exec build cmp "${src1} ${src2}"] | |
101 | set exec_output [lindex $status 1] | |
102 | set exec_output [prune_warnings $exec_output] | |
103 | ||
21c40443 | 104 | if [string equal "" $exec_output] then { |
1fafefd5 | 105 | pass "objcopy $type ($testname)" |
985e0264 AM |
106 | if { $type == "executable" } { |
107 | set dir [file dirname $t_copyfile] | |
108 | set f2 [file tail $t_copyfile] | |
109 | set status [remote_exec host find "$dir -name $f2 -newer $t_tempfile -print"] | |
110 | set exec_output [lindex $status 1] | |
111 | if [string equal "" $exec_output] then { | |
112 | pass "objcopy $type ($testname) timestamp" | |
113 | } else { | |
114 | fail "objcopy $type ($testname) timestamp" | |
115 | } | |
116 | } | |
64bb95af L |
117 | } else { |
118 | send_log "$exec_output\n" | |
119 | verbose "$exec_output" 1 | |
252b5132 | 120 | |
64bb95af L |
121 | # On OSF/1, this succeeds with gas and fails with /bin/as. |
122 | setup_xfail "alpha*-*-osf*" | |
252b5132 | 123 | |
1fafefd5 | 124 | fail "objcopy $type ($testname)" |
64bb95af | 125 | } |
252b5132 RH |
126 | } |
127 | } | |
128 | ||
6a81c59b AM |
129 | setup_xfail "hppa*-*-*" |
130 | setup_xfail "sh-*-coff*" | |
131 | setup_xfail "tic54x-*-*" | |
132 | clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*" | |
0a6a8b59 | 133 | clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "hppa*-*-*elf*" |
1fafefd5 | 134 | objcopy_test "simple copy" bintest.s object "" "" |
64bb95af | 135 | |
37d0d091 JH |
136 | # Test verilog data width |
137 | proc objcopy_test_verilog {testname} { | |
138 | global OBJCOPY | |
139 | global OBJCOPYFLAGS | |
140 | global srcdir | |
141 | global subdir | |
142 | global copyfile | |
143 | set binfile tmpdir/verilogtest.o | |
144 | set verilog tmpdir/verilog | |
145 | ||
146 | set got [binutils_assemble $srcdir/$subdir/verilogtest.s $binfile] | |
147 | if {![binutils_assemble $srcdir/$subdir/verilogtest.s $binfile]} then { | |
17e04eff | 148 | unsupported "objcopy ($testname)" |
37d0d091 JH |
149 | return |
150 | } | |
151 | ||
152 | set got [binutils_run $OBJCOPY "-O verilog $binfile $verilog"] | |
153 | if ![string equal "" $got] then { | |
154 | fail "objcopy ($testname)" | |
155 | } | |
156 | ||
157 | set got [binutils_run $OBJCOPY "-O verilog --verilog-data-width 0 $binfile $verilog-0.hex"] | |
158 | if ![regexp "verilog data width must be at least 1 byte" $got] then { | |
159 | fail "objcopy ($testname 0) {$got}" | |
160 | } else { | |
161 | pass "objcopy ($testname 0)" | |
162 | } | |
163 | ||
164 | foreach width {1 2 4 8} { | |
165 | set got [binutils_run $OBJCOPY "-O verilog --verilog-data-width $width $binfile $verilog-$width.hex"] | |
166 | if ![string equal "" $got] then { | |
167 | fail "objcopy ($testname $width)" | |
168 | } | |
169 | send_log "regexp_diff $verilog-$width.hex $srcdir/$subdir/verilog-$width.hex\n" | |
170 | if {! [regexp_diff "$verilog-$width.hex" "$srcdir/$subdir/verilog-$width.hex"]} { | |
171 | pass "objcopy ($testname $width)" | |
172 | } else { | |
173 | fail "objcopy ($testname $width)" | |
174 | } | |
175 | } | |
176 | } | |
177 | ||
178 | objcopy_test_verilog "verilog data width" | |
179 | ||
96037eb0 NC |
180 | if { [file exists $tempfile] } { |
181 | # Test reversing bytes in a section. | |
9e48b4c6 | 182 | |
96037eb0 NC |
183 | set reversed ${tempfile}-reversed |
184 | set sect_names [get_standard_section_names] | |
185 | if { $sect_names != "" } { | |
9e7ed8b0 JDA |
186 | if { [istarget hppa*-*-hpux*] } { |
187 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j \$PRIVATE\$ -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"] | |
188 | } else { | |
189 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"] | |
190 | } | |
96037eb0 NC |
191 | } else { |
192 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"] | |
9e48b4c6 NC |
193 | } |
194 | ||
96037eb0 NC |
195 | if ![string equal "" $got] then { |
196 | fail "objcopy --reverse-bytes" | |
197 | } else { | |
198 | if [is_remote host] { | |
199 | remote_upload host ${reversed} tmpdir/copy-reversed.o | |
200 | set reversed tmpdir/copy-reversed.o | |
201 | } | |
9e48b4c6 | 202 | |
96037eb0 NC |
203 | set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"] |
204 | set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"] | |
9e48b4c6 | 205 | |
96037eb0 NC |
206 | set want "^ \[0-9\]+ (\[0-9\]+)" |
207 | set found_orig [regexp -lineanchor $want $origdata -> origdata] | |
208 | set found_rev [regexp -lineanchor $want $revdata -> revdata] | |
8941d5e8 | 209 | |
96037eb0 | 210 | if {$found_orig == 0 || $found_rev == 0} then { |
96037eb0 NC |
211 | fail "objcopy --reverse-bytes" |
212 | } else { | |
213 | scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4 | |
214 | scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1 | |
215 | ||
216 | if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then { | |
217 | pass "objcopy --reverse-bytes" | |
218 | } else { | |
219 | fail "objcopy --reverse-bytes" | |
220 | } | |
221 | } | |
9e48b4c6 NC |
222 | } |
223 | } | |
224 | ||
b7dd81f7 NC |
225 | # Test interleaved copy of multiple byte width |
226 | ||
227 | set sequence_file sequence_file | |
228 | set file [open ${sequence_file} w] | |
229 | puts ${file} "12345678" | |
230 | close ${file} | |
231 | ||
232 | if [is_remote host] { | |
233 | remote_upload host ${sequence_file} tmpdir/sequence_file | |
234 | set sequence_file tmpdir/sequence_file | |
235 | } | |
236 | ||
237 | set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"] | |
238 | ||
21c40443 | 239 | if ![string equal "" $got] then { |
b7dd81f7 NC |
240 | fail "objcopy -i --interleave-width" |
241 | } else { | |
242 | if [is_remote host] { | |
243 | remote_upload host ${copyfile} tmpdir/interleave_output | |
244 | set interleave_output tmpdir/interleave_output | |
245 | } else { | |
246 | set interleave_output ${copyfile} | |
247 | } | |
248 | ||
249 | set file [open ${interleave_output} r] | |
250 | gets $file line | |
251 | send_log "$line\n" | |
252 | verbose $line | |
253 | ||
21c40443 | 254 | if ![string equal "1256" $line] then { |
b7dd81f7 NC |
255 | fail "objcopy -i --interleave-width" |
256 | } | |
257 | pass "objcopy -i --interleave-width" | |
258 | ||
259 | close $file | |
260 | } | |
261 | ||
252b5132 RH |
262 | # Test generating S records. |
263 | ||
96037eb0 NC |
264 | if { [file exists $tempfile] } { |
265 | # We make the srec filename 8.3 compatible. Note that the header string | |
266 | # matched against depends on the name of the file. Ugh. | |
252b5132 | 267 | |
252b5132 | 268 | if [is_remote host] { |
96037eb0 NC |
269 | set srecfile copy.sre |
270 | set header_string S00B0000636F70792E737265C1 | |
271 | } else { | |
272 | set srecfile ${copyfile}.srec | |
273 | set header_string S0130000746D706469722F636F70792E7372656397 | |
252b5132 | 274 | } |
252b5132 | 275 | |
96037eb0 NC |
276 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"] |
277 | ||
278 | if ![string equal "" $got] then { | |
252b5132 RH |
279 | fail "objcopy -O srec" |
280 | } else { | |
96037eb0 NC |
281 | if [is_remote host] { |
282 | remote_upload host ${srecfile} tmpdir/copy.srec | |
283 | set srecfile tmpdir/copy.srec | |
252b5132 | 284 | } |
96037eb0 NC |
285 | set file [open ${srecfile} r] |
286 | ||
287 | # The first S record is fixed by the file name we are using. | |
288 | gets $file line | |
252b5132 RH |
289 | send_log "$line\n" |
290 | verbose $line | |
96037eb0 NC |
291 | if ![regexp "$header_string.*" $line] { |
292 | send_log "bad header\n" | |
252b5132 RH |
293 | fail "objcopy -O srec" |
294 | } else { | |
96037eb0 NC |
295 | while {[gets $file line] != -1 \ |
296 | && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} { | |
252b5132 RH |
297 | send_log "$line\n" |
298 | verbose $line | |
96037eb0 NC |
299 | set line "**EOF**" |
300 | } | |
301 | send_log "$line\n" | |
302 | verbose $line | |
303 | if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then { | |
304 | send_log "bad trailer\n" | |
252b5132 RH |
305 | fail "objcopy -O srec" |
306 | } else { | |
96037eb0 NC |
307 | if {[gets $file line] != -1} then { |
308 | send_log "garbage at end\n" | |
309 | send_log "$line\n" | |
310 | verbose $line | |
252b5132 RH |
311 | fail "objcopy -O srec" |
312 | } else { | |
96037eb0 NC |
313 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"] |
314 | if ![regexp "file format srec" $got] then { | |
315 | send_log "objdump failed\n" | |
316 | fail "objcopy -O srec" | |
317 | } else { | |
318 | pass "objcopy -O srec" | |
319 | } | |
252b5132 RH |
320 | } |
321 | } | |
322 | } | |
252b5132 | 323 | |
96037eb0 NC |
324 | close $file |
325 | } | |
252b5132 RH |
326 | } |
327 | ||
328 | # Test setting and adjusting the start address. We only test this | |
329 | # while generating S records, because we may not be able to set the | |
330 | # start address for other object file formats, and the S record case | |
331 | # is the only useful one anyhow. | |
332 | ||
333 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"] | |
334 | if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then { | |
335 | perror "objdump can not recognize bintest.o" | |
336 | set origstart "" | |
337 | } else { | |
627fe3fb | 338 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"] |
21c40443 | 339 | if ![string equal "" $got] then { |
252b5132 RH |
340 | fail "objcopy --set-start" |
341 | } else { | |
342 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"] | |
343 | if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then { | |
344 | fail "objcopy --set-start" | |
345 | } else { | |
346 | if {$srecstart != 0x7654} then { | |
347 | send_log "$srecstart != 0x7654\n" | |
348 | fail "objcopy --set-start" | |
349 | } else { | |
350 | pass "objcopy --set-start" | |
351 | } | |
352 | } | |
353 | } | |
354 | ||
627fe3fb | 355 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"] |
21c40443 | 356 | if ![string equal "" $got] then { |
252b5132 RH |
357 | fail "objcopy --adjust-start" |
358 | } else { | |
359 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"] | |
360 | if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then { | |
361 | fail "objcopy --adjust-start" | |
362 | } else { | |
363 | if {$srecstart != $origstart + 0x123} then { | |
364 | send_log "$srecstart != $origstart + 0x123\n" | |
365 | fail "objcopy --adjust-start" | |
366 | } else { | |
367 | pass "objcopy --adjust-start" | |
368 | } | |
369 | } | |
370 | } | |
371 | } | |
372 | ||
373 | # Test adjusting the overall VMA, and adjusting the VMA of a | |
374 | # particular section. We again only test this when generating S | |
375 | # records. | |
376 | ||
377 | set low "" | |
378 | set lowname "" | |
379 | ||
380 | set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"] | |
381 | ||
382 | set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)" | |
383 | ||
384 | set got $headers | |
385 | while {[regexp $headers_regexp $got all name size vma rest]} { | |
386 | set vma 0x$vma | |
387 | set size 0x$size | |
388 | if {$size != 0} { | |
389 | if {$low == "" || $vma < $low} { | |
390 | set low $vma | |
391 | set lowname $name | |
392 | } | |
393 | } | |
394 | set got $rest | |
395 | } | |
396 | ||
397 | if {$low == "" || $origstart == ""} then { | |
398 | perror "objdump can not recognize bintest.o" | |
399 | } else { | |
627fe3fb | 400 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"] |
21c40443 | 401 | if ![string equal "" $got] then { |
252b5132 RH |
402 | fail "objcopy --adjust-vma" |
403 | } else { | |
404 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"] | |
405 | set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)" | |
406 | if ![regexp $want $got all start vma] then { | |
407 | fail "objcopy --adjust-vma" | |
408 | } else { | |
409 | set vma 0x$vma | |
410 | if {$vma != $low + 0x123} then { | |
411 | send_log "$vma != $low + 0x123\n" | |
412 | fail "objcopy --adjust-vma" | |
413 | } else { | |
414 | if {$start != $origstart + 0x123} then { | |
415 | send_log "$start != $origstart + 0x123\n" | |
416 | fail "objcopy --adjust-vma" | |
417 | } else { | |
418 | pass "objcopy --adjust-vma" | |
419 | } | |
420 | } | |
421 | } | |
422 | } | |
423 | ||
424 | set arg "" | |
425 | set got $headers | |
426 | while {[regexp $headers_regexp $got all name size vma rest]} { | |
427 | set vma 0x$vma | |
428 | if {$vma == $low} then { | |
429 | set arg "$arg --adjust-section-vma $name+4" | |
430 | } | |
431 | set got $rest | |
432 | } | |
433 | ||
627fe3fb | 434 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"] |
21c40443 | 435 | if ![string equal "" $got] then { |
252b5132 RH |
436 | fail "objcopy --adjust-section-vma +" |
437 | } else { | |
438 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"] | |
439 | set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)" | |
440 | if ![regexp $want $got all vma] then { | |
441 | fail "objcopy --adjust-section-vma +" | |
442 | } else { | |
443 | set vma 0x$vma | |
444 | if {$vma != $low + 4} then { | |
445 | send_log "$vma != $low + 4\n" | |
446 | fail "objcopy --adjust-section-vma +" | |
447 | } else { | |
448 | pass "objcopy --adjust-section-vma +" | |
449 | } | |
450 | } | |
451 | } | |
452 | ||
453 | regsub -all "\\+4" $arg "=[expr $low + 4]" argeq | |
627fe3fb | 454 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"] |
21c40443 | 455 | if ![string equal "" $got] then { |
252b5132 RH |
456 | fail "objcopy --adjust-section-vma =" |
457 | } else { | |
458 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"] | |
459 | set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)" | |
460 | if ![regexp $want $got all vma] then { | |
461 | fail "objcopy --adjust-section-vma =" | |
462 | } else { | |
463 | set vma 0x$vma | |
464 | if {$vma != $low + 4} then { | |
465 | send_log "$vma != $low + 4\n" | |
466 | fail "objcopy --adjust-section-vma =" | |
467 | } else { | |
468 | pass "objcopy --adjust-section-vma =" | |
469 | } | |
470 | } | |
471 | } | |
472 | } | |
473 | ||
474 | # Test stripping an object. | |
475 | ||
476 | proc strip_test { } { | |
1983fbac | 477 | global AR |
252b5132 RH |
478 | global CC |
479 | global STRIP | |
480 | global STRIPFLAGS | |
481 | global NM | |
482 | global NMFLAGS | |
483 | global srcdir | |
484 | global subdir | |
28902555 | 485 | global READELF |
252b5132 RH |
486 | |
487 | set test "strip" | |
488 | ||
489 | if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } { | |
490 | untested $test | |
491 | return | |
492 | } | |
493 | ||
28902555 BS |
494 | set osabi_fail "false" |
495 | if [is_elf_format] { | |
496 | verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in" | |
497 | set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"] | |
498 | if { [lindex $exec_output 0] != 0 } then { | |
17e04eff | 499 | fail "$test preserving OS/ABI" |
28902555 BS |
500 | set osabi_fail "true" |
501 | } else { | |
502 | verbose -log "grep OS/ABI tmpdir/osabi.in" | |
503 | catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in | |
504 | } | |
505 | } | |
506 | ||
252b5132 | 507 | if [is_remote host] { |
1983fbac | 508 | set archive libstrip.a |
8d263650 | 509 | set objfile [remote_download host tmpdir/testprog.o] |
1983fbac L |
510 | remote_file host delete $archive |
511 | } else { | |
512 | set archive tmpdir/libstrip.a | |
513 | set objfile tmpdir/testprog.o | |
514 | } | |
515 | ||
516 | remote_file build delete tmpdir/libstrip.a | |
517 | ||
518 | set exec_output [binutils_run $AR "rc $archive ${objfile}"] | |
7dd36a6f | 519 | set exec_output [prune_warnings $exec_output] |
21c40443 | 520 | if ![string equal "" $exec_output] { |
1983fbac | 521 | fail $test |
21c40443 | 522 | unresolved "$test preserving OS/ABI" |
1983fbac L |
523 | return |
524 | } | |
525 | ||
8648f88f | 526 | set exec_output [binutils_run $STRIP "-g $archive"] |
7dd36a6f | 527 | set exec_output [prune_warnings $exec_output] |
21c40443 | 528 | if ![string equal "" $exec_output] { |
8648f88f | 529 | fail $test |
21c40443 | 530 | unresolved "$test preserving OS/ABI" |
8648f88f L |
531 | return |
532 | } | |
533 | ||
1983fbac | 534 | set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"] |
7dd36a6f | 535 | set exec_output [prune_warnings $exec_output] |
21c40443 | 536 | if ![string equal "" $exec_output] { |
1983fbac | 537 | fail $test |
21c40443 | 538 | unresolved "$test preserving OS/ABI" |
1983fbac L |
539 | return |
540 | } | |
541 | ||
28902555 BS |
542 | if { $osabi_fail != "true" && [is_elf_format] } { |
543 | verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out" | |
544 | set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"] | |
545 | if { [lindex $exec_output 0] != 0 } then { | |
17e04eff | 546 | fail "$test preserving OS/ABI" |
28902555 BS |
547 | } else { |
548 | verbose -log "grep OS/ABI tmpdir/osabi.out" | |
549 | catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out | |
550 | if { "$osabi_in" == "$osabi_out" } { | |
551 | pass "$test preserving OS/ABI" | |
552 | } else { | |
553 | fail "$test preserving OS/ABI" | |
554 | } | |
555 | } | |
556 | } | |
557 | ||
1983fbac L |
558 | if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } { |
559 | untested $test | |
560 | return | |
561 | } | |
562 | ||
563 | if [is_remote host] { | |
8d263650 | 564 | set objfile [remote_download host tmpdir/testprog.o] |
252b5132 RH |
565 | } else { |
566 | set objfile tmpdir/testprog.o | |
567 | } | |
568 | ||
569 | set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"] | |
7dd36a6f | 570 | set exec_output [prune_warnings $exec_output] |
21c40443 | 571 | if ![string equal "" $exec_output] { |
252b5132 RH |
572 | fail $test |
573 | return | |
574 | } | |
575 | ||
576 | set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"] | |
7dd36a6f | 577 | set exec_output [prune_warnings $exec_output] |
68a4c073 | 578 | if ![string match "*: no symbols*" $exec_output] { |
252b5132 RH |
579 | fail $test |
580 | return | |
581 | } | |
582 | ||
583 | pass $test | |
584 | } | |
585 | ||
586 | strip_test | |
587 | ||
588 | # Test stripping an object file with saving a symbol | |
589 | ||
590 | proc strip_test_with_saving_a_symbol { } { | |
591 | global CC | |
592 | global STRIP | |
593 | global STRIPFLAGS | |
594 | global NM | |
595 | global NMFLAGS | |
596 | global srcdir | |
597 | global subdir | |
598 | ||
599 | set test "strip with saving a symbol" | |
600 | ||
601 | if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } { | |
602 | untested $test | |
603 | return | |
604 | } | |
605 | ||
606 | if [is_remote host] { | |
8d263650 | 607 | set objfile [remote_download host tmpdir/testprog.o] |
252b5132 RH |
608 | } else { |
609 | set objfile tmpdir/testprog.o | |
610 | } | |
611 | ||
612 | set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"] | |
7dd36a6f | 613 | set exec_output [prune_warnings $exec_output] |
21c40443 | 614 | if ![string equal "" $exec_output] { |
252b5132 RH |
615 | fail $test |
616 | return | |
617 | } | |
618 | ||
619 | set exec_output [binutils_run $NM "$NMFLAGS $objfile"] | |
7dd36a6f | 620 | set exec_output [prune_warnings $exec_output] |
195fe636 | 621 | if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \ |
252b5132 RH |
622 | && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} { |
623 | fail $test | |
624 | return | |
625 | } | |
626 | ||
627 | pass $test | |
628 | } | |
629 | ||
630 | strip_test_with_saving_a_symbol | |
631 | ||
632 | # Build a final executable. | |
633 | ||
d4f5b5e2 AM |
634 | set exe [exeext] |
635 | set test_prog "testprog${exe}" | |
f8a7038a | 636 | |
252b5132 RH |
637 | proc copy_setup { } { |
638 | global srcdir | |
639 | global subdir | |
9a580d9a | 640 | global gcc_gas_flag |
f8a7038a | 641 | global test_prog |
72d4c02f | 642 | global host_triplet |
21c40443 | 643 | |
8d263650 BE |
644 | set res [build_wrapper testglue.o] |
645 | set flags { debug } | |
21c40443 | 646 | |
f1ac0afe | 647 | if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] && ![istarget arm*-*-uclinuxfdpiceabi] } { |
903b1f5b NS |
648 | return 1 |
649 | } | |
21c40443 | 650 | |
252b5132 | 651 | if { $res != "" } { |
8d263650 BE |
652 | lappend flags "additional_flags=[lindex $res 1]" |
653 | set add_libs "testglue.o" | |
252b5132 | 654 | } else { |
8d263650 | 655 | set add_libs "" |
252b5132 RH |
656 | } |
657 | ||
5940a93c TS |
658 | if { [istarget *-*-linux*] |
659 | || [istarget *-*-gnu*] } { | |
9a580d9a L |
660 | foreach i $gcc_gas_flag { |
661 | set flags "additional_flags=$i $flags" | |
662 | } | |
663 | } | |
f8a7038a | 664 | if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } { |
252b5132 RH |
665 | return 2 |
666 | } | |
667 | ||
8d263650 BE |
668 | set result [remote_load target tmpdir/$test_prog] |
669 | set status [lindex $result 0] | |
252b5132 RH |
670 | |
671 | if { $status != "pass" } { | |
72d4c02f | 672 | send_log "cannot run executable, status = ${status} on ${host_triplet}\n" |
21c40443 | 673 | return 3 |
252b5132 RH |
674 | } |
675 | ||
676 | return 0 | |
677 | } | |
678 | ||
679 | # Test copying an executable. | |
680 | ||
681 | proc copy_executable { prog flags test1 test2 } { | |
f8a7038a | 682 | global test_prog |
252b5132 RH |
683 | |
684 | if [is_remote host] { | |
8d263650 | 685 | set testfile [remote_download host tmpdir/$test_prog] |
252b5132 RH |
686 | set testcopy copyprog |
687 | } else { | |
f8a7038a | 688 | set testfile tmpdir/$test_prog |
252b5132 RH |
689 | set testcopy tmpdir/copyprog |
690 | } | |
8d263650 | 691 | remote_file host delete $testcopy |
252b5132 RH |
692 | |
693 | set exec_output [binutils_run $prog "$flags $testfile $testcopy"] | |
694 | ||
21c40443 | 695 | if ![string equal "" $exec_output] { |
252b5132 | 696 | fail $test1 |
21c40443 | 697 | if [string equal "" $test2] { |
0d063f63 AM |
698 | return |
699 | } | |
252b5132 RH |
700 | fail $test2 |
701 | return | |
702 | } | |
703 | ||
704 | if [is_remote host] { | |
705 | remote_upload host $testcopy tmpdir/copyprog | |
706 | } | |
707 | ||
f8a7038a | 708 | set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"] |
8d263650 | 709 | set exec_output [lindex $status 1] |
252b5132 | 710 | |
21c40443 | 711 | if [string equal "" $exec_output] then { |
252b5132 RH |
712 | pass $test1 |
713 | } else { | |
714 | send_log "$exec_output\n" | |
715 | verbose "$exec_output" | |
716 | ||
717 | # This will fail for many reasons. For example, it will most | |
718 | # likely fail if a non-GNU linker is used. Therefore, we do | |
719 | # not insist that it pass. If you are using an assembler and | |
720 | # linker based on the same BFD as objcopy, it is worth | |
721 | # investigating to see why this failure occurs. If we are | |
722 | # cross compiling, we assume that a GNU linker is being used, | |
723 | # and expect it to succeed. | |
724 | if {[isnative]} then { | |
725 | setup_xfail "*-*-*" | |
726 | } | |
727 | ||
9cc0123f | 728 | # This also fails for some mips targets. See elf32-mips.c |
252b5132 | 729 | # mips_elf_sym_is_global. |
9cc0123f AM |
730 | if { [is_bad_symtab] } then { |
731 | setup_xfail "*-*-*" | |
732 | } | |
252b5132 | 733 | |
11d47adc | 734 | setup_xfail "arm*-*-pe" |
88f14853 | 735 | setup_xfail "*-*-mingw*" |
899396fe | 736 | setup_xfail "*-*-cygwin*" |
21c40443 | 737 | |
252b5132 RH |
738 | fail $test1 |
739 | } | |
740 | ||
21c40443 | 741 | if [string equal "" $test2] { |
0d063f63 AM |
742 | return |
743 | } | |
744 | ||
252b5132 | 745 | set output [remote_load target tmpdir/copyprog] |
8d263650 | 746 | set status [lindex $output 0] |
252b5132 RH |
747 | if { $status != "pass" } { |
748 | fail $test2 | |
749 | } else { | |
750 | pass $test2 | |
751 | } | |
752 | } | |
753 | ||
754 | # Test stripping an executable | |
755 | ||
21c40443 | 756 | proc strip_executable { prog flags test1 test2 } { |
252b5132 RH |
757 | global NM |
758 | global NMFLAGS | |
28902555 | 759 | global READELF |
252b5132 | 760 | |
0fcc17f8 | 761 | remote_file build delete tmpdir/striprog |
252b5132 RH |
762 | remote_download build tmpdir/copyprog tmpdir/striprog |
763 | if [is_remote host] { | |
8d263650 | 764 | set copyfile [remote_download host tmpdir/striprog] |
252b5132 RH |
765 | } else { |
766 | set copyfile tmpdir/striprog | |
767 | } | |
768 | ||
28902555 BS |
769 | set osabi_fail "false" |
770 | if [is_elf_format] { | |
771 | verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in" | |
772 | set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"] | |
773 | if { [lindex $exec_output 0] != 0 } then { | |
17e04eff | 774 | fail "$test1 preserving OS/ABI" |
28902555 BS |
775 | set osabi_fail "true" |
776 | } else { | |
777 | verbose -log "grep OS/ABI tmpdir/osabi.in" | |
778 | catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in | |
779 | } | |
780 | } | |
781 | ||
252b5132 | 782 | set exec_output [binutils_run $prog "$flags ${copyfile}"] |
21c40443 AM |
783 | if ![string equal "" $exec_output] { |
784 | fail $test1 | |
785 | if [string equal "" $test2] { | |
786 | return | |
787 | } | |
788 | fail $test2 | |
252b5132 RH |
789 | return |
790 | } | |
791 | ||
792 | if [is_remote host] { | |
8d263650 | 793 | remote_upload host ${copyfile} tmpdir/striprog |
252b5132 RH |
794 | } |
795 | ||
28902555 BS |
796 | if { $osabi_fail != "true" && [is_elf_format] } { |
797 | verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out" | |
798 | set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"] | |
799 | if { [lindex $exec_output 0] != 0 } then { | |
17e04eff | 800 | fail "$test1 preserving OS/ABI" |
28902555 BS |
801 | } else { |
802 | verbose -log "grep OS/ABI tmpdir/osabi.out" | |
803 | catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out | |
804 | if { "$osabi_in" == "$osabi_out" } { | |
21c40443 | 805 | pass "$test1 preserving OS/ABI" |
28902555 | 806 | } else { |
21c40443 | 807 | fail "$test1 preserving OS/ABI" |
28902555 BS |
808 | } |
809 | } | |
810 | } | |
811 | ||
252b5132 | 812 | set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"] |
68a4c073 | 813 | if ![string match "*: no symbols*" $exec_output] { |
21c40443 | 814 | fail $test1 |
252b5132 RH |
815 | return |
816 | } | |
21c40443 AM |
817 | |
818 | if [string equal "" $test2] { | |
819 | return | |
820 | } | |
821 | ||
822 | set result [remote_load target tmpdir/striprog] | |
823 | set status [lindex $result 0] | |
824 | if { $status != "pass" } { | |
825 | fail $test2 | |
826 | return | |
827 | } | |
828 | ||
829 | pass $test2 | |
252b5132 RH |
830 | } |
831 | ||
832 | # Test stripping an executable with saving a symbol | |
833 | ||
21c40443 | 834 | proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } { |
252b5132 RH |
835 | global NM |
836 | global NMFLAGS | |
837 | ||
0fcc17f8 | 838 | remote_file build delete tmpdir/striprog |
252b5132 RH |
839 | remote_download build tmpdir/copyprog tmpdir/striprog |
840 | if [is_remote host] { | |
8d263650 | 841 | set copyfile [remote_download host tmpdir/striprog] |
252b5132 RH |
842 | } else { |
843 | set copyfile tmpdir/striprog | |
844 | } | |
845 | ||
846 | set exec_output [binutils_run $prog "$flags ${copyfile}"] | |
21c40443 AM |
847 | if ![string equal "" $exec_output] { |
848 | fail $test1 | |
849 | if [string equal "" $test2] { | |
850 | return | |
851 | } | |
852 | fail $test2 | |
252b5132 RH |
853 | return |
854 | } | |
855 | ||
252b5132 | 856 | set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"] |
edc45ab9 HPN |
857 | if { [istarget mmix-knuth-mmixware] } { |
858 | # Whenever there's a symbol in the mmo format, there's the symbol | |
859 | # Main, so remove it manually from the expected output for sake of | |
860 | # this test. | |
861 | ||
862 | # Using "" not {} to get the \n and \r translated. | |
863 | regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output | |
864 | } | |
865 | ||
adacfc81 JJ |
866 | if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \ |
867 | && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} { | |
21c40443 | 868 | fail $test1 |
252b5132 RH |
869 | return |
870 | } | |
21c40443 AM |
871 | |
872 | if [string equal "" $test2] { | |
873 | return | |
874 | } | |
875 | ||
876 | if [is_remote host] { | |
877 | remote_upload host ${copyfile} tmpdir/striprog | |
878 | } | |
879 | ||
880 | set result [remote_load target tmpdir/striprog] | |
881 | set status [lindex $result 0] | |
882 | if { $status != "pass" } { | |
883 | fail $test2 | |
884 | return | |
885 | } | |
886 | ||
887 | pass $test2 | |
252b5132 RH |
888 | } |
889 | ||
11701589 JK |
890 | # Test keeping only debug symbols of an executable |
891 | ||
892 | proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } { | |
0fcc17f8 | 893 | remote_file build delete tmpdir/striprog |
11701589 JK |
894 | remote_download build tmpdir/copyprog tmpdir/striprog |
895 | if [is_remote host] { | |
896 | set copyfile [remote_download host tmpdir/striprog] | |
897 | } else { | |
898 | set copyfile tmpdir/striprog | |
899 | } | |
900 | ||
901 | set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"] | |
21c40443 | 902 | if ![string equal "" $exec_output] { |
11701589 JK |
903 | fail $test1 |
904 | return | |
905 | } | |
906 | pass $test1 | |
907 | ||
908 | set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"] | |
21c40443 | 909 | if ![string equal "" $exec_output] { |
11701589 JK |
910 | fail $test2 |
911 | return | |
912 | } | |
913 | pass $test2 | |
914 | } | |
915 | ||
63b9bbb7 | 916 | # Tests that in a debug only copy of a file the sections |
de194d85 | 917 | # headers whose types have been changed to NOBITS still |
63b9bbb7 NC |
918 | # retain their sh_link fields. |
919 | ||
920 | proc keep_debug_symbols_and_check_links { prog flags test } { | |
921 | global READELF | |
922 | ||
923 | remote_file build delete tmpdir/striprog | |
924 | remote_download build tmpdir/copyprog tmpdir/striprog | |
925 | if [is_remote host] { | |
926 | set copyfile [remote_download host tmpdir/striprog] | |
927 | } else { | |
928 | set copyfile tmpdir/striprog | |
929 | } | |
930 | ||
931 | set exec_output [binutils_run $prog "$flags ${copyfile}"] | |
21c40443 | 932 | if ![string equal "" $exec_output] { |
63b9bbb7 NC |
933 | fail $test |
934 | return | |
935 | } | |
936 | ||
937 | set got [binutils_run $READELF "-S --wide ${copyfile}"] | |
938 | ||
939 | set fails 0 | |
940 | # Regexp to match a section with NOBITS type and extract its name and sh_link fields | |
941 | while {[regexp \ | |
942 | {[^a-zA-Z]+([a-zA-Z0-9_\.]+)[ ]+NOBITS[ ]+[0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9]+[ A]+([0-9]+)(.*)} \ | |
943 | $got all name link rest]} { | |
944 | set sh_link 0x$link | |
945 | if {$sh_link == 0} { | |
946 | # Only some NOBITS sections should have a non-zero sh_link field. | |
947 | # Look for them by name. | |
948 | verbose "NOBITS section .$name has a 0 sh_link field\n" | |
949 | switch $name { | |
950 | "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
951 | "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
952 | "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
953 | "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
954 | "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
955 | "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
956 | "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
957 | "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
958 | } | |
959 | } | |
960 | set got $rest | |
961 | } | |
962 | ||
963 | if {$fails == 0} { | |
964 | pass $test | |
965 | } else { | |
966 | fail $test | |
967 | } | |
968 | } | |
969 | ||
970 | ||
252b5132 | 971 | set test1 "simple objcopy of executable" |
21c40443 AM |
972 | set test1r "run objcopy of executable" |
973 | set test2 "strip executable" | |
974 | set test2r "run stripped executable" | |
975 | set test3 "strip executable with saving a symbol" | |
976 | set test3r "run stripped executable with saving a symbol" | |
977 | set test4 "keep only debug data" | |
978 | set test5 "simple objcopy of debug data" | |
63b9bbb7 | 979 | if [is_elf_format] { |
21c40443 | 980 | set test6 "NOBITS sections retain sh_link field" |
63b9bbb7 | 981 | } |
252b5132 RH |
982 | |
983 | switch [copy_setup] { | |
984 | "1" { | |
985 | # do nothing | |
986 | } | |
987 | "2" { | |
988 | untested $test1 | |
21c40443 | 989 | untested $test1r |
252b5132 | 990 | untested $test2 |
21c40443 | 991 | untested $test2r |
252b5132 | 992 | untested $test3 |
21c40443 | 993 | untested $test3r |
252b5132 | 994 | untested $test4 |
11701589 | 995 | untested $test5 |
63b9bbb7 | 996 | if [is_elf_format] { |
21c40443 | 997 | untested $test6 |
63b9bbb7 | 998 | } |
252b5132 RH |
999 | } |
1000 | "3" { | |
0d063f63 | 1001 | copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "" |
21c40443 AM |
1002 | unsupported $test1r |
1003 | strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "" | |
1004 | unsupported $test2r | |
1005 | strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "" | |
1006 | unsupported $test3r | |
1007 | keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \ | |
1008 | "$OBJCOPY" "$OBJCOPYFLAGS" "$test5" | |
63b9bbb7 | 1009 | if [is_elf_format] { |
21c40443 | 1010 | keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6" |
63b9bbb7 | 1011 | } |
252b5132 RH |
1012 | } |
1013 | "0" { | |
21c40443 AM |
1014 | copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r" |
1015 | strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r" | |
1016 | strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r" | |
1017 | keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \ | |
1018 | "$OBJCOPY" "$OBJCOPYFLAGS" "$test5" | |
63b9bbb7 | 1019 | if [is_elf_format] { |
21c40443 | 1020 | keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6" |
63b9bbb7 | 1021 | } |
252b5132 RH |
1022 | } |
1023 | } | |
ad2fb2cd | 1024 | |
6a0d0afd L |
1025 | proc objcopy_test_readelf {testname srcfile} { |
1026 | global OBJCOPY | |
1027 | global OBJCOPYFLAGS | |
1028 | global READELF | |
1029 | global srcdir | |
1030 | global subdir | |
1031 | ||
1032 | if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then { | |
17e04eff | 1033 | unsupported "objcopy ($testname)" |
6a0d0afd L |
1034 | return |
1035 | } | |
1036 | ||
1037 | verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o" | |
7f6a71ff JM |
1038 | set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"] |
1039 | if { [lindex $exec_output 0] != 0 | |
21c40443 | 1040 | || ![string equal "" [lindex $exec_output 1]] } then { |
6a0d0afd | 1041 | fail "objcopy ($testname)" |
004314cc | 1042 | return |
6a0d0afd L |
1043 | } |
1044 | ||
1045 | verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out" | |
7f6a71ff JM |
1046 | set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"] |
1047 | if { [lindex $exec_output 0] != 0 } then { | |
17e04eff | 1048 | fail "objcopy ($testname)" |
7f6a71ff JM |
1049 | return |
1050 | } | |
1051 | set exec_output [prune_warnings [lindex $exec_output 1]] | |
21c40443 | 1052 | if ![string equal "" $exec_output] then { |
6a0d0afd L |
1053 | unresolved "objcopy ($testname)" |
1054 | return | |
1055 | } | |
1056 | ||
1057 | verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out" | |
7f6a71ff JM |
1058 | set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"] |
1059 | if { [lindex $exec_output 0] != 0 } then { | |
17e04eff | 1060 | fail "objcopy ($testname)" |
7f6a71ff JM |
1061 | return |
1062 | } | |
1063 | set exec_output [prune_warnings [lindex $exec_output 1]] | |
21c40443 | 1064 | if ![string equal "" $exec_output] then { |
6a0d0afd L |
1065 | unresolved "objcopy ($testname)" |
1066 | return | |
1067 | } | |
1068 | ||
1069 | verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out" | |
1070 | catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output | |
1071 | set exec_output [prune_warnings $exec_output] | |
1072 | ||
21c40443 | 1073 | if [string equal "" $exec_output] then { |
6a0d0afd L |
1074 | pass "objcopy ($testname)" |
1075 | } else { | |
1076 | fail "objcopy ($testname)" | |
1077 | } | |
1078 | } | |
1079 | ||
0b45135e AB |
1080 | proc objcopy_test_symbol_manipulation {} { |
1081 | global srcdir | |
1082 | global subdir | |
1083 | ||
1084 | set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]] | |
1085 | foreach t $test_list { | |
1086 | # We need to strip the ".d", but can leave the dirname. | |
1087 | verbose [file rootname $t] | |
1088 | run_dump_test [file rootname $t] | |
1089 | } | |
1090 | } | |
1091 | ||
b8871f35 L |
1092 | proc objcopy_test_elf_common_symbols {} { |
1093 | global srcdir | |
1094 | global subdir | |
1095 | ||
1096 | # hpux has a non-standard common directive. | |
1097 | if { [istarget "*-*-hpux*"] } then { | |
1098 | return | |
1099 | } | |
1100 | ||
1101 | set test_list [lsort [glob -nocomplain $srcdir/$subdir/common-*.d]] | |
1102 | foreach t $test_list { | |
1103 | # We need to strip the ".d", but can leave the dirname. | |
1104 | verbose [file rootname $t] | |
1105 | run_dump_test [file rootname $t] | |
1106 | } | |
1107 | } | |
1108 | ||
ad2fb2cd L |
1109 | # ia64 specific tests |
1110 | if { ([istarget "ia64-*-elf*"] | |
1111 | || [istarget "ia64-*-linux*"]) } { | |
1fafefd5 | 1112 | objcopy_test "ia64 link order" link-order.s object "" "" |
64bb95af | 1113 | } |
ad2fb2cd | 1114 | |
64bb95af | 1115 | # ELF specific tests |
506b86a4 | 1116 | set elf64 "" |
64bb95af | 1117 | if [is_elf_format] { |
0b45135e | 1118 | objcopy_test_symbol_manipulation |
b8871f35 | 1119 | objcopy_test_elf_common_symbols |
6a81c59b AM |
1120 | |
1121 | setup_xfail "hppa*-*-*" | |
1122 | setup_xfail "sh-*-coff*" | |
1123 | setup_xfail "tic54x-*-*" | |
1124 | clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*" | |
0a6a8b59 | 1125 | clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "hppa*-*-*elf*" |
1fafefd5 | 1126 | objcopy_test "ELF unknown section type" unknown.s object "" "" |
6a81c59b | 1127 | |
a0a8a934 NC |
1128 | objcopy_test_readelf "ELF group 1" group.s |
1129 | objcopy_test_readelf "ELF group 2" group-2.s | |
1130 | objcopy_test_readelf "ELF group 3" group-3.s | |
1131 | objcopy_test_readelf "ELF group 4" group-4.s | |
a91e1603 | 1132 | objcopy_test_readelf "GNU_MBIND section" mbind1.s |
b2fc24d4 | 1133 | run_dump_test "group-5" |
4c8e8a7e | 1134 | run_dump_test "group-6" |
6e5e9d58 AM |
1135 | run_dump_test "group-7a" |
1136 | run_dump_test "group-7b" | |
1137 | run_dump_test "group-7c" | |
85d7f0b9 | 1138 | run_dump_test "copy-1" |
0930eddd | 1139 | run_dump_test "note-1" |
acea835f AM |
1140 | # Use copytest.o from the note-1 test to determine ELF32 or ELF64 |
1141 | if [is_elf64 tmpdir/copytest.o] { | |
506b86a4 | 1142 | set elf64 "--defsym ELF64=1" |
9ef920e9 | 1143 | run_dump_test "note-2-64" |
714da62f | 1144 | run_dump_test "note-3-64" |
6f156d7a | 1145 | run_dump_test "note-4-64" |
5c49f2cd | 1146 | run_dump_test "note-6-64" |
9ef920e9 NC |
1147 | } else { |
1148 | run_dump_test "note-2-32" | |
714da62f | 1149 | run_dump_test "note-3-32" |
6f156d7a | 1150 | run_dump_test "note-4-32" |
5c49f2cd | 1151 | run_dump_test "note-6-32" |
9ef920e9 | 1152 | } |
0df8ad28 | 1153 | run_dump_test "note-5" |
ad2fb2cd | 1154 | } |
af3c5dea | 1155 | |
af3c5dea | 1156 | run_dump_test "copy-2" |
81fc501a | 1157 | run_dump_test "copy-3" |
0691f7af | 1158 | run_dump_test "copy-4" |
de564eb5 | 1159 | run_dump_test "copy-5" |
e3ecd631 | 1160 | run_dump_test "copy-6" |
de564eb5 | 1161 | |
506b86a4 AM |
1162 | # Use bintest.o from the copy-4 test to determine ELF reloc type |
1163 | set reloc_format rel | |
1164 | if { [is_elf_format] && [is_rela tmpdir/bintest.o] } { | |
1165 | set reloc_format rela | |
1166 | } | |
cbd44e24 L |
1167 | run_dump_test "pr19020a" |
1168 | run_dump_test "pr19020b" | |
d58c2e3a RS |
1169 | |
1170 | if [is_elf_format] { | |
748fc5e9 L |
1171 | run_dump_test "strip-1" |
1172 | run_dump_test "strip-2" | |
1aa9ef63 | 1173 | run_dump_test "strip-3" |
eb3980ce L |
1174 | run_dump_test "strip-4" |
1175 | run_dump_test "strip-5" | |
f206e905 L |
1176 | run_dump_test "strip-6" |
1177 | run_dump_test "strip-7" | |
e1e87d1e L |
1178 | run_dump_test "strip-8" |
1179 | run_dump_test "strip-9" | |
d4ac1f87 | 1180 | run_dump_test "strip-12" |
2f8ceb38 MR |
1181 | |
1182 | if { [istarget "mips64*-*-openbsd*"] } { | |
1183 | set reloc_format mips64 | |
2f8ceb38 | 1184 | } |
506b86a4 | 1185 | # A relocation type not supported by any target |
fbaf61ad NC |
1186 | if { [istarget "nds32*-*"] } { |
1187 | set reloc 255 | |
1188 | } else { | |
1189 | set reloc 215 | |
1190 | } | |
506b86a4 AM |
1191 | run_dump_test "strip-13" [list \ |
1192 | [list source strip-13${reloc_format}.s] \ | |
1193 | [list as "${elf64} --defsym RELOC=${reloc}"]] | |
3f97ba9f MR |
1194 | # Select a relocation number that corresponds to one actually |
1195 | # supported by the target and ABI being tested. | |
1196 | if { [istarget "aarch64*-*"] } { | |
1197 | set reloc 259 | |
1198 | } elseif { [istarget "ia64*-*"] \ | |
1199 | || [istarget "m32r*-*"] \ | |
d4ae1932 | 1200 | || [istarget "nds32*-*"] \ |
3f97ba9f MR |
1201 | || [istarget "v850*-*"] } { |
1202 | set reloc 50 | |
d4ae1932 AM |
1203 | } elseif { [istarget "pru-*"] } { |
1204 | set reloc 11 | |
3f97ba9f MR |
1205 | } else { |
1206 | set reloc 1 | |
1207 | } | |
1208 | run_dump_test "strip-14" [list \ | |
506b86a4 AM |
1209 | [list source strip-14${reloc_format}.s] \ |
1210 | [list as "${elf64} --defsym RELOC=${reloc}"]] | |
d52e3d06 | 1211 | run_dump_test "strip-15" [list \ |
506b86a4 AM |
1212 | [list source strip-15${reloc_format}.s] \ |
1213 | [list as "${elf64} --defsym RELOC=${reloc}"]] | |
f3185997 | 1214 | |
a43942db MR |
1215 | # This requires STB_GNU_UNIQUE support with OSABI set to GNU. |
1216 | if { [supports_gnu_unique] } { | |
4cd28456 AM |
1217 | run_dump_test "strip-10" |
1218 | } | |
211dc24b | 1219 | run_dump_test "strip-11" |
312aaa3c | 1220 | |
b8b6abe0 AM |
1221 | if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } { |
1222 | # Check to make sure we don't strip a symbol named in relocations. | |
1223 | set test "objcopy keeps symbols needed by relocs" | |
312aaa3c | 1224 | |
b8b6abe0 | 1225 | set srcfile $srcdir/$subdir/needed-by-reloc.s |
312aaa3c | 1226 | |
b8b6abe0 | 1227 | if {![binutils_assemble $srcfile tmpdir/bintest.o]} then { |
17e04eff | 1228 | unsupported $test |
b8b6abe0 AM |
1229 | } else { |
1230 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"] | |
312aaa3c | 1231 | |
b8b6abe0 AM |
1232 | if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] { |
1233 | pass $test | |
1234 | } else { | |
1235 | fail $test | |
1236 | } | |
1237 | } | |
312aaa3c | 1238 | } |
b8b6abe0 | 1239 | |
8560e02a HPN |
1240 | # The symbol table for some MIPS targets is sorted differently than |
1241 | # the ELF canonical order, so the regexps in localize-hidden-1.d fail | |
9cc0123f AM |
1242 | # to match. |
1243 | if { [is_bad_symtab] } then { | |
1244 | setup_xfail "*-*-*" | |
8560e02a | 1245 | } |
b8b6abe0 | 1246 | run_dump_test "localize-hidden-1" |
afeb3d7f L |
1247 | run_dump_test "testranges" |
1248 | run_dump_test "testranges-ia64" | |
500ee42e ILT |
1249 | |
1250 | run_dump_test "add-section" | |
2b35fb28 | 1251 | run_dump_test "add-symbol" |
500ee42e | 1252 | run_dump_test "add-empty-section" |
18ae9cc1 L |
1253 | |
1254 | run_dump_test "exclude-1a" | |
1255 | run_dump_test "exclude-1b" | |
e511c9b1 AB |
1256 | |
1257 | run_dump_test "only-section-01" | |
1258 | run_dump_test "remove-section-01" | |
d3e5f6c8 | 1259 | |
64f52b3e FS |
1260 | run_dump_test "keep-section-1" |
1261 | run_dump_test "keep-section-2" | |
1262 | ||
d3e5f6c8 AB |
1263 | # Test the remove relocation functionality |
1264 | set test_list [lsort [glob -nocomplain $srcdir/$subdir/remove-relocs-*.d]] | |
1265 | foreach t $test_list { | |
1266 | # We need to strip the ".d", but can leave the dirname. | |
1267 | verbose [file rootname $t] | |
1268 | run_dump_test [file rootname $t] | |
1269 | } | |
312aaa3c | 1270 | } |
b8b6abe0 | 1271 | run_dump_test "localize-hidden-2" |
14f2c699 L |
1272 | |
1273 | # Test objcopying an object file without global symbol | |
1274 | ||
1275 | proc objcopy_test_without_global_symbol { } { | |
1276 | global OBJCOPY | |
1277 | global OBJCOPYFLAGS | |
1278 | global OBJDUMP | |
1279 | global OBJDUMPFLAGS | |
1280 | global srcdir | |
1281 | global subdir | |
1282 | ||
1283 | set test "strip without global symbol " | |
1284 | ||
1285 | if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } { | |
1286 | untested $test | |
1287 | return | |
1288 | } | |
1289 | ||
1290 | if [is_remote host] { | |
1291 | set objfile [remote_download host tmpdir/pr19547.o] | |
1292 | } else { | |
1293 | set objfile tmpdir/pr19547.o | |
1294 | } | |
1295 | ||
1296 | set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"] | |
7dd36a6f | 1297 | set exec_output [prune_warnings $exec_output] |
14f2c699 L |
1298 | if ![string equal "" $exec_output] { |
1299 | fail $test | |
1300 | return | |
1301 | } | |
1302 | ||
1303 | set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"] | |
7dd36a6f | 1304 | set exec_output [prune_warnings $exec_output] |
14f2c699 L |
1305 | if {![regexp "no symbols" $exec_output]} { |
1306 | fail $test | |
1307 | return | |
1308 | } | |
1309 | ||
1310 | pass $test | |
1311 | } | |
1312 | ||
41699fa4 NC |
1313 | # The AArch64 and ARM targets preserve mapping symbols |
1314 | # in object files, so they will fail this test. | |
1315 | setup_xfail aarch64*-*-* arm*-*-* | |
1316 | ||
14f2c699 | 1317 | objcopy_test_without_global_symbol |
f9853190 AM |
1318 | |
1319 | # objcopy remove relocation from executable test | |
1320 | ||
1321 | proc objcopy_remove_relocations_from_executable { } { | |
1322 | global OBJCOPY | |
1323 | global srcdir | |
1324 | global subdir | |
1325 | global READELF | |
1326 | ||
1327 | set test "remove-section relocation sections" | |
1328 | ||
1329 | if { [target_compile $srcdir/$subdir/testprog.c tmpdir/pr23611 executable debug] != "" } { | |
1330 | untested $test | |
1331 | return | |
1332 | } | |
1333 | ||
1334 | if [is_remote host] { | |
1335 | set objfile [remote_download host tmpdir/pr23611] | |
1336 | } else { | |
1337 | set objfile tmpdir/pr23611 | |
1338 | } | |
1339 | set out tmpdir/pr23611.out | |
1340 | ||
1341 | set exec_output1 [binutils_run $OBJCOPY "-R .rela.plt -R .rela.dyn -R .rel.plt -R .rel.dyn $objfile $out"] | |
d4f5b5e2 AM |
1342 | |
1343 | set exec_output1 [prune_warnings $exec_output1] | |
1344 | if ![string equal "" $exec_output1] { | |
1345 | fail $test | |
1346 | return | |
1347 | } | |
1348 | ||
f9853190 AM |
1349 | set exec_output2 [binutils_run $READELF "-S $out"] |
1350 | if { [string match "*.rel.plt*" $exec_output2] || [string match "*.rela.plt*" $exec_output2] || [string match "*.rel.dyn*" $exec_output2] || [string match "*.rela.dyn*" $exec_output2] } { | |
1351 | fail $test | |
1352 | return | |
1353 | } | |
1354 | pass $test | |
1355 | } | |
1356 | ||
d4f5b5e2 AM |
1357 | if [is_elf_format] { |
1358 | objcopy_remove_relocations_from_executable | |
1359 | } | |
319dbdfb L |
1360 | |
1361 | run_dump_test "pr23633" | |
de4859ea NC |
1362 | |
1363 | run_dump_test "set-section-alignment" | |
1fafefd5 | 1364 | |
6a81c59b AM |
1365 | setup_xfail "hppa*-*-*" |
1366 | setup_xfail "sh-*-coff*" | |
0a6a8b59 | 1367 | setup_xfail "spu-*-*" |
6a81c59b | 1368 | clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*" |
0a6a8b59 | 1369 | clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "hppa*-*-*elf*" |
8eca5c7d SC |
1370 | if { [istarget pdp11-*-*] } { |
1371 | set src "pr25662-pdp11.s" | |
1372 | } else { | |
1373 | set src "pr25662.s" | |
1374 | } | |
1375 | ||
790147a9 L |
1376 | set ldflags "-T$srcdir/$subdir/pr25662.ld" |
1377 | if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } { | |
1378 | append ldflags " --disable-reloc-section" | |
1379 | } | |
1380 | ||
efd0ed58 AM |
1381 | #xcoff doesn't support arbitrary sections |
1382 | if { ![is_xcoff_format] } { | |
790147a9 | 1383 | objcopy_test "pr25662" $src executable "" $ldflags |
efd0ed58 | 1384 | } |